Similarities Between XPath Axes and Extension Methods

Extension methods are defined in the System.Xml.Linq.Extensions class that recall XPath Axes
functions. The first two methods that we will consider are Ancestors and Descendants, which
return an IEnumerable sequence of elements for a particular XNode instance.
Descendants returns all the elements after the current node in the document graph, regardless
of their depth in the graph. Ancestors is somehow complementary to Descendants and returns
all the elements before the current node in the document graph. Both are shown here:


public static IEnumerable Ancestors(
this IEnumerable source)
where T: XNode;
public static IEnumerable Ancestors(
this IEnumerable source, XName name)
where T: XNode;
public static IEnumerable Descendants(
this IEnumerable source)
where T: XContainer;
public static IEnumerable Descendants(
this IEnumerable source, XName name)
where T: XContainer;

These methods are useful for querying an XML source to find a particular element after or
before the current one, regardless of its position in the graph

No comments:

Post a Comment