Kirk Evans Blog

.NET From a Markup Perspective

XPath – What is an XmlNode, and what does node() return?

“Han” wrote:
I used the term node in general manner. And I can show you many examples of the usage in MSDN or something. The term node is frequently used as element.
There are 12 node types. The node() will return only a few of them. Then what’s your criteria of the term node? Are the rest not nodes because the node() doesn’t return them?

Everything in an XML document (whitespace, elements, attributes, processing instructions, text) is represented in the .NET framework with XmlNode. And all of those items are considered nodes in the XML DOM. The XPath node type test “node() ” tests true for all of these items as well. However, an attribute is not a child of an element. If you use the node() test in the context of a location path, the location path will default to using the child axis, thus not selecting the attributes.

Consider the following XML document:


The following XPath will not return anything, because Customers has no children:


However, if we change the XML document:


The comment is selected, because it is a child of the Customers element. So, a comment is a node, an element is a node, and an attribute is a node. The difference is that an attribute is not a child of its containing element.

You can specify the axis in the location path to retrieve the attribute:


This will return the attribute only and will not select the child comment. This is because we specified the attribute axis, and the comment is not part of the attribute axis.


Dare Obasanjo posted a follow-up to this post, explaining how entity references are treated.