4

给定以下 XML 标记:

<root xmlns="Demo">
    <child name="foo"/>
</root>

和一个XPathNavigator定位在<child>元素上,

string withNs = navigator.GetAttribute("name", navigator.NamespaceURI);
string withoutNs = navigator.GetAttribute("name", "");

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

这是为什么?我本来希望它会反过来,因为name属性必须Demochild元素一样位于命名空间中。

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

4

1 回答 1

5

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

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

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

于 2009-12-17T16:13:23.890 回答