开发者

Why do I have to pass an empty namespace to XPathNavigator.GetAttribute?

开发者 https://www.devze.com 2022-12-14 11:49 出处:网络
Given the following XML markup: <root xmlns=\"Demo\"> <child name=\"foo\"/> </root> and an XPathNavigator positioned on the <child> element,

Given the following XML markup:

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

and an XPathNavigator positioned on the <child> element,

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

produce strange results: withNs is empty, withoutNs contains foo.

Why is that? I would have expected it would be the other way round, as the name attribute must be in the Demo namespace like the child element开发者_运维技巧.

The MSDN documentation does not mention any magic meaning of passing namespaceURI="", so I assumed you have to pass the real namespace URI of the attribute.


as the name attribute must be in the Demo namespace like the child element.

Attributes do not inherit the namespace of the element to which they belong, as per the w3c specification, and that is why you got those results, which are correct.

Related article: http://web.archive.org/web/20170118162309/http://www.xmlplease.com/attributexmlns

0

精彩评论

暂无评论...
验证码 换一张
取 消