tags:

views:

3921

answers:

7

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.

A: 

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.

I'm checking it out now. will update.

A: 

Maybe trough the XmlNamespaceManager ? http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.removenamespace.aspx but it's just a guess.

Matthieu
I've tried this, but haven't been able to figure out how to remove a namespace with this class.
axk
+1  A: 

.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.

Greg
I'm not sure, but as long as there is no positive answer, I believe it is true.
axk
+1  A: 

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.

Vin
Thanks for the suggestion. Will definitely give it a try.
axk
A: 

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.

A: 

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.

ALI SHAH
A: 

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);
            }
Matt Harris