Get list ancestor elements in CSharp

Description

The following code shows how to get list ancestor elements.

Example


  /*w w  w.j  av  a 2 s. c o m*/
  
  
    
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
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())
            Console.WriteLine(el.Name);
   }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style