Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference XElement Nodes() vs Elements()?

Tags:

c#

.net

xelement

Documentation says:


XContainer.Nodes Method () Returns a collection of the child nodes of this element or document, in document order.

Remarks Note that the content does not include attributes. In LINQ to XML, attributes are not considered to be nodes of the tree. They are name/value pairs associated with an element.

XContainer.Elements Method () Returns a collection of the child elements of this element or document, in document order.


So it looks like Nodes() has a limitation, but then why does it exist? Are there any possible reasons or advantages of using Nodes()?

like image 382
osexpert Avatar asked Jun 10 '16 09:06

osexpert


2 Answers

The reason is simple: XNode is a base (abstract) class for all xml "parts", and XElement is just one such part (so XElement is subclass of XNode). Consider this code:

XDocument doc = XDocument.Parse("<root><el1 />some text<!-- comment --></root>");
foreach (var node in doc.Root.Nodes()) {
      Console.WriteLine(node);
}
foreach (var element in doc.Root.Elements()) {
      Console.WriteLine(element);
}

Second loop (over Elements()) will only return one item: <el />

First loop however will return also text node (some text) and comment node (<!-- comment -->), so you see the difference.

You can see what other descendants of XNode there are in documentaiton of XNode class.

like image 41
Evk Avatar answered Sep 18 '22 20:09

Evk


It's not the case that Nodes "have a limitation". Nodes are the fundamental building block on which most other things (including Elements) are built.

The XML document is represented as a hierarchy (tree), and the nodes are used to represent the fundamental structure of the hierarchy.

If we consider the following XML document:

<root>
  <element>
    <child>
     Text
    </child>
  </element>
  <!-- comment -->
  <element>
    <child>
      Text
    <child>
  </element>
</root>

Clearly the whole document cannot be represented as elements, since the comment and the text within the "child" elements are not elements. Instead, it's represented as a hierarchy of nodes.

In this document, there are 5 elements (the root element, two "element" elements and two "child" elements). All of these are nodes, but there are also 3 other nodes: the text within "child" elements, and the comment.

It's misleading to say that nodes have a "limitation" because they don't have attributes. Only elements have attributes, and elements are nodes! But there are other nodes (e.g. the comment) that can't have attributes. So not all types of node have attributes.

In coding terms, Node is the base class on which higher-level types such as Element are built. If you want to enumerate the elements in the document, then using XContainer.Elements() is a nice shortcut to do that - but you could also use XContainer.Nodes() and get all the nodes, including both the elements and the other stuff. (You can check the type of the node to see whether you have an element node, a text node, or whatever; if it's an element, you can up-cast it).

like image 174
Gary McGill Avatar answered Sep 18 '22 20:09

Gary McGill