7

Please help me to remove xmlns namespace from the WEB API Response.

Adding,

config.Formatters.XmlFormatter.UseXmlSerializer = true;

(or)

[DataContract(Namespace="")]

didn't help me. Your help is much appreciated.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Pitchai P
  • 1,317
  • 10
  • 22

1 Answers1

8

Finally, I found the solution. Just created a CustomXmlFormatter to remove the namespace from the root element.

public class IgnoreNamespacesXmlMediaTypeFormatter : XmlMediaTypeFormatter
{
 public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
    try
    {
        var task = Task.Factory.StartNew(() =>
        {
            var xns = new XmlSerializerNamespaces();
            var serializer = new XmlSerializer(type);
            xns.Add(string.Empty, string.Empty);
            serializer.Serialize(writeStream, value, xns);
        });

        return task;
    }
    catch (Exception)
    {
        return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
    }
   }
}
Pitchai P
  • 1,317
  • 10
  • 22