Changing Namespace Declarations in an XML Document

Changing Namespace Declarations in an XML Document

The XmlDocument exposes namespace declarations and xmlns attributes as part of the document object model. These are stored in the XmlDocument, so when you save the document, it can preserve the location of those attributes. Changing these attributes has no affect on the Name, NamespaceURI, and Prefix properties of other nodes already in the tree. For example, if you load the following document:

<test xmlns="123"/>

Then the test element has NamespaceURI 123. If you remove the xmlns attribute as follows:

doc.documentElement.RemoveAttribute("xmlns")
[C#]
doc.documentElement.RemoveAttribute("xmlns");

Then the test element still has the NamespaceURI of 123. Likewise if you add a different xmlns attribute to the doc element as follows:

doc.documentElement.SetAttribute("xmlns","456");
[C#]
doc.documentElement.SetAttribute("xmlns","456");

Then the test element still has NamespaceURI 123. So changing xmlns attributes will have no affect until you save and re-load the XmlDocument object.

See Also

XML Document Object Model (DOM)

Show:
© 2016 Microsoft