XElement.AncestorsAndSelf(XName) returns a filtered collection of elements that contain this element
using System;
using System.Linq;
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("Child",
new XElement("GrandChild", "element content")
)
);
XElement gc = xmlTree.Element("Child").Element("GrandChild");
IEnumerable<XElement> aas = gc.AncestorsAndSelf("Child");
foreach (XElement el in aas)
Console.WriteLine(el.Name);
}
}
Related examples in the same category