Remove xmlns:i and xmlns from webapi
Asked Answered
D

1

7

I have been asked to provide the following XML document from an http endpoint, exactly like:-

<?xml version="1.0" encoding="utf-8"?> 
  <XMLFile xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SalesOrders>
 ...
</SalesOrders>

However Web API spits out

<?xml version="1.0" encoding="utf-8"?>
<XMLFile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://schemas.datacontract.org/2004/07/White.Label.Ordering.Infrastructure.Data.Queries.Export">
    <SalesOrders>
        ...
    </SalesOrders>

I have google around and tried various fixes but to no avail, my model looks like

[DataContract]
public class XMLFile
{
    [DataMember]
    public List<SalesOrder> SalesOrders { get; set; }
}

[DataContract]
public class SalesOrder
{
    [DataMember(Order = 1)]
    public string OrderNumber { get; set; }
}

and my set up lools like this

    public static void Register(HttpConfiguration config)
    {
        config.Formatters.XmlFormatter.WriterSettings.OmitXmlDeclaration = false;
        ...

    }

How do I remove xmlns:i and xmlns and replace with xmlns:xsd and xmlns:xsi?

I know this is a bad question as it shouldn't matter but my consuming client is barfing.

Dugger answered 1/11, 2016 at 16:31 Comment(2)
Possible duplicate of #12591301 ?Noctule
No saw and tried that, wish it wasDugger
L
4

If you need your XML to look exactly like something, then you might be better off with XmlSerializer. DataContractSerializer doesn't give you the same level of control as it's rather assumed you use it on both ends.

That said, I would imagine your consuming client is 'barfing' because the two instances are semantically different. The first has an empty default namespace, and the second has a default namespace of http://schemas.datacontract.org/2004/07/White.Label.Ordering.Infrastructure.Data.Queries.Export.

This should be the only thing you need to correct, which you can do by setting the namespace of the DataContract.

[DataContract(Namespace="")]
public class XMLFile
{
    [DataMember]
    public List<SalesOrder> SalesOrders { get; set; }
}

[DataContract(Namespace="")]
public class SalesOrder
{
    [DataMember(Order = 1)]
    public string OrderNumber { get; set; }
}

This will give you:

<?xml version="1.0" encoding="utf-8"?>
<XMLFile xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <SalesOrders>
        ...
    </SalesOrders>
</XMLFile>
Lizzettelizzie answered 2/11, 2016 at 9:9 Comment(5)
Thanks, I have tried this on the XMLFile class, but did not add the Namesspace to all the underlying class namespaces. Will ask the client to try again. ThanksDugger
Ah crap the consuming client is still barfing. Stupid antiquated xml systems, looks like I need to go down the XMLSerializer pathDugger
Went down the XMLSerializer instead, stupid means I can't use webapi anymore to future proof it for content type negotiation. ThanksDugger
Is there any way we can remove xmlns:i also from response message ?Noseband
@RanjithMurthy it's not something you can control. And it shouldn't matter. The only way I can think to remove it would be to parse the result again using e.g. XDocument and remove the attribute there.Lizzettelizzie

© 2022 - 2025 — McMap. All rights reserved.