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.

2
  • Post your solution as an answer. Not in the question. Commented Apr 29, 2013 at 7:26
  • Thanks arulmr. Please keep editing my posts. This would be very helpful. Commented Apr 29, 2013 at 9:41

1 Answer 1

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);
    }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.