How to remove xmlns attribute with .NET XML API
Solution 1
.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.
Solution 2
Many thanks to Ali Shah, this thread solved my problem perfectly! here's a C# conversion:
var dom = new XmlDocument();
dom.Load("C:/ExampleFITrade.xml));
var loaded = new XDocument();
if (dom.DocumentElement != null)
if( dom.DocumentElement.NamespaceURI != String.Empty)
{
dom.LoadXml(dom.OuterXml.Replace(dom.DocumentElement.NamespaceURI, ""));
dom.DocumentElement.RemoveAllAttributes();
loaded = XDocument.Parse(dom.OuterXml);
}
Solution 3
Wasn't this supposed to remove namespaces?
XmlNamespaceManager mgr = new XmlNamespaceManager("xmlnametable");
mgr.RemoveNamespace("prefix", "uri");
But anyway on a tangent here, the XElement, XDocument and XNameSpace 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.
Solution 4
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:
'Remove the Equifax / Transunian / Experian root node attribute that have xmlns and load xml without xmlns attributes.
If objXMLDom.DocumentElement.NamespaceURI <> String.Empty Then
objXMLDom.LoadXml(objXMLDom.OuterXml.Replace(objXMLDom.DocumentElement.NamespaceURI, ""))
objXMLDom.DocumentElement.RemoveAllAttributes()
ResponseXML = objXMLDom.OuterXml
End If
There is no need to do anything else to remove xmlns from xml.
Solution 5
public static string RemoveXmlns(string xml)
{
//Prepare a reader
StringReader stringReader = new StringReader(xml);
XmlTextReader xmlReader = new XmlTextReader(stringReader);
xmlReader.Namespaces = false; //A trick to handle special xmlns attributes as regular
//Build DOM
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlReader);
//Do the job
xmlDocument.DocumentElement.RemoveAttribute("xmlns");
//Prepare a writer
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
//Optional: Make an output nice ;)
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.IndentChar = ' ';
xmlWriter.Indentation = 2;
//Build output
xmlDocument.Save(xmlWriter);
return stringWriter.ToString();
}
axk
Updated on July 09, 2022Comments
-
axk almost 2 years
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:
XmlDocument doc = new XmlDocument(); doc.InnerXml = @"<Element1 attr1=""value1"" xmlns=""http://mynamespace.com/"" attr2=""value2""/>"; 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"]);
The resulting output is
xmlns attr before removal=System.Xml.XmlAttribute xmlns attr after removal= <Element1 attr1="value1" xmlns="http://mynamespace.com/" />
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.
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.
@Edit: I'm talking about .NET 2.0.
-
axk over 15 yearsI've tried this, but haven't been able to figure out how to remove a namespace with this class.
-
axk over 15 yearsThanks for the suggestion. Will definitely give it a try.
-
axk over 15 yearsI'm not sure, but as long as there is no positive answer, I believe it is true.
-
Rob over 11 yearsA little narrative description of your proposed answer would be helpful.