Creating XML Elements Without Namespace Declarations

I am trying to add a new "PropertyGroup" element to the following XML file

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <PropertyGroup> <ProjectGuid>{00AA7ECA-95A0-0000-0000-AD4D2E056BFE}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>testnS</RootNamespace> <AssemblyName>testname</AssemblyName> <PluginId>4B84E5C0-EC2B-4C0C-8B8E-3FAEB09F74C6</PluginId> </PropertyGroup> </Project> 

The code I use is as follows (note that other logic has been removed):

  XmlTextReader reader = new XmlTextReader(filename); XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); XmlElement root = doc.DocumentElement; XmlNode refNode = root.SelectSingleNode("x:Project", ns); root.SelectSingleNode("Project", ns); XmlElement newElement = doc.CreateElement("PropertyGroup", "http://schemas.microsoft.com/developer/msbuild/2003"); newElement.InnerXml = "<value>test</value>"; root.InsertAfter(newElement, refNode); doc.Save(filename); 

This gives the following new element in the XML document:

 <PropertyGroup> <value xmlns="">test</value> </PropertyGroup> 

How do I get rid of a namespace declaration in an element?

+8
c # xml
source share
2 answers

You need to specify an XML namespace for all elements added to the DOM:

 XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); XmlElement root = doc.DocumentElement; XmlNode refNode = root.SelectSingleNode("x:Project", ns); XmlElement newElement = doc.CreateElement( "PropertyGroup", "http://schemas.microsoft.com/developer/msbuild/2003"); var value = newElement.AppendChild(doc.CreateElement( "value", "http://schemas.microsoft.com/developer/msbuild/2003")); value.AppendChild(doc.CreateTextNode("test")); root.InsertAfter(newElement, refNode); 

If you do not do this for any element (or if you use InnerXml ), this element will receive an empty namespace.

+10
source share

The reason this happens is because you defined the default namespace for the document as " http://schemas.microsoft.com/developer/msbuild/2003 ", the namespace definition in the node root directory:

 xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 

Then you go on to add an element that does not have a namespace (blank namespace) in the document. It must be qualified with

 xmlns="" 

because if it werenโ€™t, it would mean that the new item was in the previously mentioned Microsoft namespace - that it wasnโ€™t (or rather, you didnโ€™t ask for it).

So either:

  • you really want the new item to be in the Microsoft namespace - in which case you need to say that. The easiest way is to use createElement and provide a namespace, although you can probably specify this explicitly with the xmlns attribute on your InnerXml (which is not a particularly good way to add nodes).

  • You really need this element in an empty namespace, in which case you are probably better off qualifying all other nodes that are not in the empty namespace with the Prefix namespace.

I suspect you need the first one.

A brief overview of namespaces can be found here .

+3
source share

All Articles