1

I am using C# and XDcoument to add nodes the root element. I use this code:

XElement miAnimalNuevo = new XElement("PrincipalNode",
                new XAttribute("Atribute1", "value attribute 1"),
                new XAttribute("Attribute2", "value attribute 2"),
                new XElement("subNode","0000"));

But I get this:

<PrincipalNode Atribute1="value attribute 1" Attribute2="value attribute 2" xmlns="">
    <subNode>0000</subNode>
  </PrincipalNode>

After the attribute 2, I see the xmlns="". Why? I only want the attributes.

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 2
    http://stackoverflow.com/questions/5955878/xelement-is-automatically-adding-xmlns-to-itself – Habib May 21 '13 at 07:36

1 Answers1

3

This happens when you have an XML document that has a namespace defined somewhere up the tree.

Adding an element that is not in that namespace but in the empty namespace (i.e., no namespace) will add an empty xmlns attribute.

<xml xmlns="some_namespace_uri">
  <foo>The foo element inherits the 'some_namespace_uri' namespace</foo>
  <bar xmlns="">The bar element is in no namespace</bar>
</xml>

Related: Is xmlns="" a valid xml namespace?

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628