开发者

Detecting existence of empty XML node attribute with XPathNavigator

开发者 https://www.devze.com 2023-03-13 12:24 出处:网络
Is there an easy way to detect the existence of an empty attribute on an XPat开发者_JAVA百科hNavigator XML node (e.g. <node> vs. <node attribute=\"\">)? Node.GetAttribute returns an empty

Is there an easy way to detect the existence of an empty attribute on an XPat开发者_JAVA百科hNavigator XML node (e.g. <node> vs. <node attribute="">)? Node.GetAttribute returns an empty string either way. The only thing I can think of is checking the Node.OuterXML property, which seems like a really dumb approach.

(Note: the node won't actually be empty, so I can't just use Node.HasAttributes.)


You can use XPath:

elem.SelectSingleNode("@attribute")

This returns null, if the the attributte attribute doesn't exist, and another XPathNavigator with NodeType of Attribute and Value containing an empty string if the attribute exists, but is empty.

0

精彩评论

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