8

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.

1
  • I just ran into this issue. Good find! Commented Oct 23, 2009 at 21:41

9 Answers 9

3

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);
            }
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

I'm not sure, but as long as there is no positive answer, I believe it is true.
2

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.

1 Comment

Thanks for the suggestion. Will definitely give it a try.
2

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.

Comments

2
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();
}

1 Comment

A little narrative description of your proposed answer would be helpful.
0

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.

Comments

0

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

1 Comment

I've tried this, but haven't been able to figure out how to remove a namespace with this class.
0

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.

Comments

0

here is my solution on vb.net guys!

   Dim pathXmlTransformado As String = "C:\Fisconet4\process\11790941000192\2015\3\28\38387-1\38387_transformado.xml"
    Dim nfeXML As New XmlDocument
    Dim loaded As New XDocument

    nfeXML.Load(pathXmlTransformado)

    nfeXML.LoadXml(nfeXML.OuterXml.Replace(nfeXML.DocumentElement.NamespaceURI, ""))
    nfeXML.DocumentElement.RemoveAllAttributes()

    Dim dhCont As XmlNode = nfeXML.CreateElement("dhCont")
    Dim xJust As XmlNode = nfeXML.CreateElement("xJust")
    dhCont.InnerXml = 123
    xJust.InnerXml = 123777

    nfeXML.GetElementsByTagName("ide")(0).AppendChild(dhCont)
    nfeXML.GetElementsByTagName("ide")(0).AppendChild(xJust)

    nfeXML.Save("C:\Fisconet4\process\11790941000192\2015\3\28\38387-1\teste.xml")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.