XElement.AncestorsAndSelf(XName) returns a filtered collection of elements that contain this element : Ancestors « XML LINQ « C# / C Sharp






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

1.An Example of Calling the First Ancestors Prototype
2.Without Calling the Ancestors Operator
3.A More Concise Example of Calling the First Ancestors Prototype
4.Calling the Second Ancestors Prototype
5.XElement.AncestorsAndSelf returns a collection of elements