1
votes

How can i remove the root xml namespace given that I am using asp.net core and the XmlDataContractSerializerOutputFormatter to format the response.

All of the returned xml docs have the following format

<?xml version="1.0" encoding="utf-8"?>
   <response xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      some other stuff
   </response>

I need to remove the xmlns:i="http://www.w3.org/2001/XMLSchema-instance" part.

2

2 Answers

1
votes

Please try following code:

xmlDoc.Load(@"FILE_PATH");
XmlNodeList header_el = xmlDoc.GetElementsByTagName("response");

foreach (XmlNode child in header_el)
{
  if (child.Attributes[0] != null)
  child.Attributes.Remove(child.Attributes[0]);
}
Console.WriteLine(xmlDoc.OuterXml);

Output of this code is:

<?xml version="1.0" encoding="utf-8"?><response>
   some other stuff
</response>
1
votes

I ended up extending the existing XmlDataContractSerializerOutputFormatter, tapping into the method that creates the xmlwriter object and wrapping it in an adapter to edit the functionality, since editing the response wasn't really a practical nor feasible solution due to the structure of the response object as represented by the framework.

First the Formatter

public class CustomDataContractOutputFormatter : XmlDataContractSerializerOutputFormatter
{
    public CustomDataContractOutputFormatter() : base(){}
    public CustomDataContractOutputFormatter(XmlWriterSettings set) : base(set){}
    public override XmlWriter CreateXmlWriter(TextWriter writer, XmlWriterSettings xmlWriterSettings)
    {
        var bas = base.CreateXmlWriter(writer, xmlWriterSettings);
        var ret = new XmlWriterNoNamespace();
        ret.AdaptedWriter = bas;
        return ret;
    }
}

Then the base adapter (part of it)

public class XmlWriterAdapter : XmlWriter
{
    XmlWriter _adaptedWriter;
    public XmlWriterAdapter():base(){}
    public XmlWriter AdaptedWriter 
    {
        get
        {
            return _adaptedWriter;
        } 
        set
        {
            this._adaptedWriter = value;
        }
    }
    public override WriteState WriteState { get{return _adaptedWriter.WriteState;} }
    public override void Flush()
    {
        _adaptedWriter.Flush();
    }
    public override string LookupPrefix(string ns)
    {
        return _adaptedWriter.LookupPrefix(ns);
    }
}

And finally the specific adapter with a hack to disable writing of the namespace

public class XmlWriterNoNamespace : XmlWriterAdapter
{
    bool _skipAttribute;
    public XmlWriterNoNamespace():base(){}
    public override void WriteEndAttribute()
    {
        if(_skipAttribute)
        {
            _skipAttribute = false;
            return;
        }
        base.WriteEndAttribute();
    }
    public override void WriteStartAttribute(string prefix, string localName, string ns)
    {
        if(prefix.Equals("xmlns"))
        {
            _skipAttribute = true;
            return;
        }
        base.WriteStartAttribute(prefix, localName, ns);
    }
    public override void WriteString(string text)
    {
        if(_skipAttribute)
            return;
        base.WriteString(text);
    }
}