0

I'm using .NET's XmlDocument class because I need to edit XML documents dynamically.

I'm trying to create an element that looks like this:

<element xmlns:abc="MyNamespace">

Here's the code I've written so far:

XmlDocument xml = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("abc", "MyNamespace");

XmlNode declarationNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(declarationNode);

// Create a root element with a default namespace.
XmlNode rootNode = xml.CreateElement("root", "rootNamespace");
xml.AppendChild(rootNode);

XmlNode containerNode = xml.CreateElement("container", xml.DocumentElement.NamespaceURI);
rootNode.AppendChild(containerNode);

// Create the element node in question:
XmlNode elementNode = xml.CreateElement("element", xml.DocumentElement.NamespaceURI);

XmlAttribute attr = xml.CreateAttribute("abc", "def", nsmgr.LookupNamespace("abc"));

elementNode.Attributes.Append(attr);

containerNode.AppendChild(elementNode);

Here is my output:

<?xml version="1.0" encoding="UTF-8"?>
<rootNode xmlns="MyNamespace">
  <container>
    <element abc:def="" xmlns:abc="MyNamespace" />
  </container>
</rootNode>

It appears the attribute attr is causing both "abc:def=""" and "xmlns:abc="MyNamespace"" to be set--all I want is for the latter?

har07
  • 88,338
  • 12
  • 84
  • 137
Justin Shidell
  • 588
  • 1
  • 5
  • 16

2 Answers2

1

This is working here

    XmlDocument xml = new XmlDocument();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
    nsmgr.AddNamespace("abc", "MyNamespace");

    XmlNode declarationNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
    xml.AppendChild(declarationNode);

    // Create a root element with a default namespace.
    XmlElement rootNode = xml.CreateElement("root");
    rootNode.SetAttribute("xmlns:abc", "MyNamespace");

    xml.AppendChild(rootNode);

    XmlNode containerNode = xml.CreateElement("container", xml.DocumentElement.NamespaceURI);
    rootNode.AppendChild(containerNode);

    // Create the element node in question:
    XmlNode elementNode = xml.CreateElement("abc", "element", "MyNamespace");

    containerNode.AppendChild(elementNode);

    Console.Write(rootNode.OuterXml); 

and the output is

 <root xmlns:abc="MyNamespace"><container><abc:element /></container></root>
Laurent Lequenne
  • 902
  • 5
  • 13
0

You should declare your namespaces at the top level.

See this post to get an example : How do I add multiple namespaces to the root element with XmlDocument?

Community
  • 1
  • 1
Laurent Lequenne
  • 902
  • 5
  • 13
  • Thanks for the link--but I think part of my problem is that the attribute in question is "xmlns", which implies a namespace, and not just any attribute that we could define. – Justin Shidell Apr 09 '15 at 16:40
  • If you declare your namespaces with prefixes at the toplevel of your document, the namespace will not been output at each node of your XML. This output doesn't need the namespace spec as it has been declared on a top level of your xml – Laurent Lequenne Apr 09 '15 at 16:41
  • if is your root element then you can only have this... but if your complete XML is like you have to specify namespace on the elements and it will not be repeated on every abc;element :=) – Laurent Lequenne Apr 09 '15 at 17:10
  • Thanks for the help, @Laurent Lequenne. I updated my source, as it still doesn't appear to be behaving the way I expect. Setting an attribute is causing two namespace decorations to be added? – Justin Shidell Apr 09 '15 at 17:33