<p>XmlElement.Attributes.Remove* methods are working fine for arbitrary attributes resulting in the removed attributes being removed from XmlDocument.OuterXml property. Xmlns attribute however is different. Here is an example:</p>

<pre><code>XmlDocument doc = new XmlDocument();
doc.InnerXml = @"&lt;Element1 attr1=""value1"" xmlns=""http://mynamespace.com/"" attr2=""value2""/&gt;";
doc.DocumentElement.Attributes.RemoveNamedItem("attr2");
Console.WriteLine("xmlns attr before removal={0}", doc.DocumentElement.Attributes["xmlns"]);
doc.DocumentElement.Attributes.RemoveNamedItem("xmlns");
Console.WriteLine("xmlns attr after removal={0}", doc.DocumentElement.Attributes["xmlns"]);
</code></pre>

<p>The resulting output is</p>

<pre><code>xmlns attr before removal=System.Xml.XmlAttribute
xmlns attr after removal=
&lt;Element1 attr1="value1" xmlns="http://mynamespace.com/" /&gt;
</code></pre>

<p>The attribute seems to be removed from the Attributes collection, but it is not removed from XmlDocument.OuterXml.
I guess it is because of the special meaning of this attribute.</p>

<p>The question is how to remove the xmlns attribute using .NET XML API.
Obviously I can just remove the attribute from a String representation of this, but I wonder if it is possible to do the same thing using the API.</p>

<p>@Edit: I'm talking about .NET 2.0.</p>

[{'answer_id': 76513, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Yes, because its an ELEMENT name, you can't explicitly remove it. Using XmlTextWriter's WriteStartElement and WirteStartAttribute, and replacing the attribute with empty spaces will likely to get the job done. </p>\n\n<p>I'm checking it out now. will update.</p>\n"}, {'answer_id': 76716, 'author': 'MatthieuGD', 'author_id': 3109, 'author_profile': 'https://Stackoverflow.com/users/3109', 'pm_score': 0, 'selected': False, 'text': '<p>Maybe trough the XmlNamespaceManager ? <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.removenamespace.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.removenamespace.aspx</a> but it\'s just a guess.</p>\n'}, {'answer_id': 76929, 'author': 'GregK', 'author_id': 8653, 'author_profile': 'https://Stackoverflow.com/users/8653', 'pm_score': 3, 'selected': True, 'text': '<p>.NET DOM API doesn\'t support modifying element\'s namespace which is what you are essentially trying to do. So, in order to solve your problem you have to construct a new document one way or another. You can use the same .NET DOM API and create a new element without specifying its namespace. Alternatively, you can create an XSLT stylesheet that transforms your original "namespaced" document to a new one in which the elements will be not namespace-qualified.</p>\n'}, {'answer_id': 77502, 'author': 'Vin', 'author_id': 1747, 'author_profile': 'https://Stackoverflow.com/users/1747', 'pm_score': 2, 'selected': False, 'text': '<p>Wasn\'t this supposed to remove namespaces?</p>\n\n<pre><code>XmlNamespaceManager mgr = new XmlNamespaceManager("xmlnametable");\nmgr.RemoveNamespace("prefix", "uri");\n</code></pre>\n\n<p>But anyway on a tangent here, the XElement, XDocument and <a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.aspx" rel="nofollow noreferrer">XNameSpace</a> classes from System.Xml.Linq namespace (.Net 3.0) are a better lot than the old XmlDocument model. Give it a go. I am addicted.</p>\n'}, {'answer_id': 974586, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>We can convert the xml to a string, remove the xmlns from that string, and then create another XmlDocument using this string, which will not have the namespace.</p>\n'}, {'answer_id': 1875023, 'author': 'ALI SHAH', 'author_id': 228093, 'author_profile': 'https://Stackoverflow.com/users/228093', 'pm_score': 2, 'selected': False, 'text': '<p>I saw the various options in this thread and come to solve my own solution for removing xmlns attributes in xml.  This is working properly and has no issues:</p>\n\n<pre><code>\'Remove the Equifax / Transunian / Experian root node attribute that have xmlns and load xml without xmlns attributes.\nIf objXMLDom.DocumentElement.NamespaceURI &lt;&gt; String.Empty Then\n  objXMLDom.LoadXml(objXMLDom.OuterXml.Replace(objXMLDom.DocumentElement.NamespaceURI, ""))\n  objXMLDom.DocumentElement.RemoveAllAttributes()\n  ResponseXML = objXMLDom.OuterXml\nEnd If\n</code></pre>\n\n<p>There is no need to do anything else to remove xmlns from xml.</p>\n'}, {'answer_id': 2251621, 'author': 'Matt Harris', 'author_id': 271817, 'author_profile': 'https://Stackoverflow.com/users/271817', 'pm_score': 2, 'selected': False, 'text': '<p>Many thanks to Ali Shah, this thread solved my problem perfectly!\nhere\'s a C# conversion:</p>\n\n<pre><code>var dom = new XmlDocument();\n        dom.Load("C:/ExampleFITrade.xml));\n        var loaded = new XDocument();\n        if (dom.DocumentElement != null)\n            if( dom.DocumentElement.NamespaceURI != String.Empty)\n            {\n                dom.LoadXml(dom.OuterXml.Replace(dom.DocumentElement.NamespaceURI, ""));\n                dom.DocumentElement.RemoveAllAttributes();\n                loaded = XDocument.Parse(dom.OuterXml);\n            }\n</code></pre>\n'}, {'answer_id': 14287546, 'author': 'pcmaniak', 'author_id': 1971348, 'author_profile': 'https://Stackoverflow.com/users/1971348', 'pm_score': 1, 'selected': False, 'text': '<pre><code>public static string RemoveXmlns(string xml)\n{\n    //Prepare a reader\n    StringReader stringReader = new StringReader(xml);\n    XmlTextReader xmlReader = new XmlTextReader(stringReader);\n    xmlReader.Namespaces = false; //A trick to handle special xmlns attributes as regular\n    //Build DOM\n    XmlDocument xmlDocument = new XmlDocument();\n    xmlDocument.Load(xmlReader);\n    //Do the job\n    xmlDocument.DocumentElement.RemoveAttribute("xmlns"); \n    //Prepare a writer\n    StringWriter stringWriter = new StringWriter();\n    XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);\n    //Optional: Make an output nice ;)\n    xmlWriter.Formatting = Formatting.Indented;\n    xmlWriter.IndentChar = \' \';\n    xmlWriter.Indentation = 2;\n    //Build output\n    xmlDocument.Save(xmlWriter);\n    return stringWriter.ToString();\n}\n</code></pre>\n'}, {'answer_id': 29321085, 'author': 'Rodrigo Serzedello', 'author_id': 2053300, 'author_profile': 'https://Stackoverflow.com/users/2053300', 'pm_score': 0, 'selected': False, 'text': '<p>here is my solution on vb.net guys!</p>\n\n<pre><code>   Dim pathXmlTransformado As String = "C:\\Fisconet4\\process\\11790941000192\\2015\\3\\28\\38387-1\\38387_transformado.xml"\n    Dim nfeXML As New XmlDocument\n    Dim loaded As New XDocument\n\n    nfeXML.Load(pathXmlTransformado)\n\n    nfeXML.LoadXml(nfeXML.OuterXml.Replace(nfeXML.DocumentElement.NamespaceURI, ""))\n    nfeXML.DocumentElement.RemoveAllAttributes()\n\n    Dim dhCont As XmlNode = nfeXML.CreateElement("dhCont")\n    Dim xJust As XmlNode = nfeXML.CreateElement("xJust")\n    dhCont.InnerXml = 123\n    xJust.InnerXml = 123777\n\n    nfeXML.GetElementsByTagName("ide")(0).AppendChild(dhCont)\n    nfeXML.GetElementsByTagName("ide")(0).AppendChild(xJust)\n\n    nfeXML.Save("C:\\Fisconet4\\process\\11790941000192\\2015\\3\\28\\38387-1\\teste.xml")\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76424', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/578/']