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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With