为什么要添加xmlns=""在节点的属性中

本文关键字:quot 节点 属性 添加 为什么 xmlns | 更新日期: 2023-09-27 18:14:48

我使用c#和xdocument为根元素添加节点。我使用以下代码:

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="。为什么?我只需要属性。

谢谢。

为什么要添加xmlns=""在节点的属性中

当XML文档在树的某个位置定义了名称空间时,就会发生这种情况。

添加不在该命名空间中但在空命名空间中的元素(即no namespace)将添加一个空的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="有效的XML名称空间?