CSharp - Call Ancestors method on the results of Descendants

Description

Call Ancestors method on the results of Descendants

Demo

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

class Program/*from   w  ww .j a v  a 2s .  com*/
{
    static void Main(string[] args){
        XDocument xDocument = new XDocument(
          new XElement("Books",
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "Joe"),
              new XElement("LastName", "Ruby")),
            new XElement("Book",
              new XAttribute("type", "Editor"),
              new XElement("FirstName", "PHP"),
              new XElement("LastName", "Python"))));
        
        foreach (XElement element in
          xDocument.Element("Books").Descendants("FirstName").Ancestors())
        {
          Console.WriteLine("Ancestor element: {0}", element.Name);
        }
    }
}

Result

Related Topic