CSharp - Traversing Down Recursively from an XElement Object via the DescendantsAndSelf Method

Description

Traversing Down Recursively from an XElement Object via the DescendantsAndSelf Method

Demo

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

class Program/*from   www.  j a v  a 2s .  c om*/
{
    static void Main(string[] args){
              XElement firstParticipant;
        
              //  A full document with all the bells and whistles.
              XDocument xDocument = new XDocument(
                new XDeclaration("1.0", "UTF-8", "yes"),
                new XDocumentType("Books", null, "Books.dtd", null),
                new XProcessingInstruction("BookCataloger", "out-of-print"),
                //  Notice on the next line that we are saving off a reference to the first
                //  Book element.
                new XElement("Books", firstParticipant =
                  new XElement("Book",
                    new XComment("This is a new author."),
                    new XProcessingInstruction("AuthorHandler", "new"),
                    new XAttribute("type", "Author"),
                    new XElement("FirstName",
                      new XText("Joe"),
                      new XElement("NickName", "Joey")),
                    new XElement("LastName", "Ruby")),
            new XElement("Book",
              new XAttribute("type", "Editor"),
              new XElement("FirstName", "PHP"),
              new XElement("LastName", "Python"))));
        
        foreach (XElement element in firstParticipant.DescendantsAndSelf())
        {
          Console.WriteLine(element.Name);
        }
    }
}

Result

Related Topic