Get a filtered collection of the ancestor elements in CSharp
Description
The following code shows how to get a filtered collection of the ancestor elements.
Example
/* ww w . ja va2 s.c o m*/
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass{
public static void Main(){
XElement xmlTree = new XElement("Root",
new XElement("A",
new XElement("GrandChild", "content")
)
);
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");
foreach (XElement el in grandChild.Ancestors("Child"))
Console.WriteLine(el.Name);
}
}