3

Is there an easy way to detect the existence of an empty attribute on an XPathNavigator 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.)

4
  • If possible, I would recommend using LINQ to XML. Commented Jun 14, 2011 at 22:59
  • That might be doable, but in testing LINQ in the early stages of this project, I found it added a lot of overhead compared to just parsing the XML directly. It's definitely useful in some situations, though, so I may look into it some more. Commented Jun 15, 2011 at 0:14
  • I don't understand, using LINQ to XML, is “just parsing the XML directly” too, it's just different API (and IMHO better one). I don't know why would it create a performance overhead. If you were comparing it with something like XmlReader, that would be different, but you aren't. Commented Jun 16, 2011 at 22:09
  • @svick: I'd have to go back and re-try...I did preliminary evaluations on the various XML parsing options when I was starting and picked the one that seemed the fastest and that fit my needs, but I can't claim to be significantly familiar with any of them, as this is my first foray into .NET territory, so perhaps I was doing something less-than-optimal or thought there was some feature missing that was actually there. At this point, I couldn't tell you. (And yeah, my wording was a little inaccurate there, as using any parsing API is inherently not parsing the XML directly.) Commented Jun 17, 2011 at 5:32

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.