Mega Code Archive

 
Categories / C# / XML LINQ
 

XElement AncestorsAndSelf returns a collection of elements

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 = from el in gc.AncestorsAndSelf()                                     select el;         foreach (XElement el in aas)             Console.WriteLine(el.Name);     } }