Remove xmlns: me and xmlns from webapi

I was asked to provide the following XML document from the http endpoint, exactly the same: -

<?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, the 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 settings like this

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

How to remove xmlns:i and xmlns and replace with xmlns:xsd and xmlns:xsi ?

I know this is a bad question as it doesn’t matter, but my consumer client works.

+5
source share
1 answer

If you need your XML to look like something, then you might be better off with XmlSerializer . DataContractSerializer does not give you the same level of control as the assumption that you are using it at both ends.

However, I would suggest that your consumer client is "barfing" because the two instances are semantically different from each other. The first has an empty default namespace, and the second has a default namespace http://schemas.datacontract.org/2004/07/White.Label.Ordering.Infrastructure.Data.Queries.Export .

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

 [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> 
+2
source

Source: https://habr.com/ru/post/1259115/


All Articles