为什么我必须将空名称空间传递给XPathNavigator.GetAttribute?

Jen*_*ann 4 .net xml xml-namespaces xpathnavigator

给定以下XML标记:

<root xmlns="Demo">
    <child name="foo"/>
</root>
Run Code Online (Sandbox Code Playgroud)

并且XPathNavigator位于<child>元素上,

string withNs = navigator.GetAttribute("name", navigator.NamespaceURI);
string withoutNs = navigator.GetAttribute("name", "");
Run Code Online (Sandbox Code Playgroud)

产生奇怪的结果:withNs是空的,withoutNs包含foo.

这是为什么?我原以为它会反过来,因为name属性必须Demochild元素一样在命名空间中.

MSDN文档没有提到传递的任何神奇含义namespaceURI="",所以我假设你必须传递属性的真实命名空间URI.

bil*_*ham 5

因为name属性必须Demochild元素一样位于命名空间中.

根据w3c规范,属性不会继承它们所属元素的命名空间,这就是为什么你得到那些正确的结果的原因.

相关文章:http://web.archive.org/web/20170118162309/http : //www.xmlplease.com/attributexmlns