2013-05-21 40 views
1

我正在使用C#和XDcoument向根节点添加节点。我用这个代码:为什么要在节点的属性中添加xmlns =“”?

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

但我得到这个:

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

属性2后,我看到的xmlns = “”。为什么?我只想要属性。

谢谢。

+2

http://stackoverflow.com/questions/5955878/xelement-is-automatically-adding-xmlns-to-itself – Habib

回答

3

如果您有一个名称空间在树的某个位置定义的XML文档,就会发生这种情况。

添加一个不在该名称空间中但在空名称空间中的元素(即,没有名称空间)将添加一个空的xmlns属性。

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

相关:Is xmlns="" a valid xml namespace?