Search code examples
c#.netxmldatacontractserializer

How to set Namespace for DataContract?


I have a situation where I have a REST controller that is called by a user and this controller then requests data from upstream and receives JSON as a response. This JSON is then transformed into XML and send back to the user as a response. The problem is that I am not able to set specific namespace for the XML root element. I am using DataContractSerializer.

I am not really experienced with .NET and have previously worked mainly with JSON so i am a bit lost as to what to try next.

I have tried to set the namespace using ContractNamespaceAttribute like:

[assembly: ContractNamespaceAttribute("http://schemas.datacontract.org/2004/07/APIBridge.Models", ClrNamespace = "APIBridge.Models")]
namespace APIBridge.Models
{
  [DataContract]
  public class Order
  {
    // DataMembers here...
  }
}

And I also tried setting the namespace in the DataContracAttribute like:

namespace APIBridge.Models
{
  [DataContract(Name = "Order", Namespace = 
  "http://schemas.datacontract.org/2004/07/APIBridge.Models")]
  public class Order
  {
    // Datamembers here...
  }
}

How I would like the namespace to be set is:

<ArrayOfOrder xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/APIBridge.Models">

But the actual result is:

<ArrayOfOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractattribute?redirectedfrom=MSDN&view=netframework-4.8

In the DataContractAttribute documentation above it says:

"By default, when you apply the DataContractAttribute to a class, it uses the class name as the local name and the class's namespace (prefixed with "http://schemas.datacontract.org/2004/07/") as the namespace URI."

This actually would be the desired result but also as a default namespace I get the same result that is mentioned above. Why is this?

Any ideas?

UPDATE: Below requested service operation

        public List<Order> loadOrdersBasic(UserOptions userOpts)
        {
            List<Order> orders = new List<Order>();
            HttpClient httpClient = AuthenticationHelper.CreateHttpClient(userOpts, options);
            String url = String.Format("api/orders?supplier_no={0}", userOpts.SupplierId);
            HttpResponseMessage response = httpClient.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                orders = response.Content.ReadAsAsync<List<Order>>().Result;
            }
            else {
                throw new ServiceException(getHttpErrorMessage(url, response));
            }
            return orders;
        }

Solution

  • Answering to my own question.

    Turned out that I had missed one line in my configuration file where XmlMediaTypeFormatter was set to use XmlSerializer instead of DataContractSerializer. This overrode the default namespace.