A Simple XML Query Using LINQ to XML

   
 

using System;
using System.Linq;
using System.Xml.Linq;

public class MainClass {
    public static void Main() {

        XElement books = XElement.Parse(
          @"<books>
        <book>
          <title>P</title>
          <author>J</author>
        </book>
        <book>
          <title>W</title>
          <author>B</author>
        </book>
        <book>
          <title>C</title>
          <author>A</author>
        </book>
    </books>");

        var titles =
         from book in books.Elements("book")
         where (string)book.Element("author") == "J"
         select book.Element("title");

        foreach (var title in titles)
            Console.WriteLine(title.Value);
    }
}

    


Using the OfType Operator to Return Just the Comments

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XElement firstParticipant;
        XDocument xDocument =  new XDocument(
         new XDeclaration("1.0", "UTF-8", "yes"),
         new XDocumentType("Books", null, "Books.dtd", null),
         new XProcessingInstruction("Book", "out-of-print"),
       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", "J"),
         new XElement("LastName", "R")),
         new XElement("Book",
         new XAttribute("type", "Author"),
         new XElement("FirstName", "E"),
         new XElement("LastName", "B"))));
        foreach (XNode node in firstParticipant.Nodes().OfType<XComment>()) {
            Console.WriteLine(node);
        }
    }
}

    


Using the OfType Operator to Return Just the Elements

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {

        XElement firstParticipant;
        XDocument xDocument =  new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
          new XDocumentType("Books", null, "Books.dtd", null),
          new XProcessingInstruction("Book", "out-of-print"),
        new XElement("Books", firstParticipant =
          new XElement("Book",
        new XComment("a new author."),
        new XProcessingInstruction("AuthorHandler", "new"),
        new XAttribute("type", "Author"),
        new XElement("FirstName", "A"),
        new XElement("LastName", "B")),
        new XElement("Book",
        new XAttribute("type", "Author"),
        new XElement("FirstName", "C"),
        new XElement("LastName", "D"))));

        foreach (XNode node in firstParticipant.Nodes().OfType<XElement>()) {
            Console.WriteLine(node);
        }
    }
}

    


Traversing Forward from the Current Node Using the NodesAfterSelf Method

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XElement firstParticipant;
        XDocument xDocument = new XDocument(
         new XDeclaration("1.0", "UTF-8", "yes"),
         new XDocumentType("Books", null, "Books.dtd", null),
         new XProcessingInstruction("Book", "out-of-print"),
       new XElement("Books",
       new XComment("Begin Of List"), firstParticipant =
       new XElement("Book",
       new XAttribute("type", "Author"),
       new XElement("FirstName", "J"),
       new XElement("LastName", "R")),
       new XElement("Book",
       new XAttribute("type", "Author"),
       new XElement("FirstName", "E"),
       new XElement("LastName", "B")),
       new XComment("End Of List")));
        foreach (XNode node in firstParticipant.NodesAfterSelf()) {
            Console.WriteLine(node);
        }
    }
}

    


Traversing Backward from an XElement Object via the PreviousNode Property

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XElement firstParticipant;
        XDocument xDocument = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
         new XDocumentType("Books", null, "Books.dtd", null),
         new XProcessingInstruction("Book", "out-of-print"),
         new XElement("Books", firstParticipant =
          new XElement("Book",
         new XAttribute("type", "Author"),
         new XElement("FirstName", "J"),
         new XElement("LastName", "R")),
         new XElement("Book",
         new XAttribute("type", "Author"),
         new XElement("FirstName", "E"),
         new XElement("LastName", "B"))));
        Console.WriteLine(firstParticipant.NextNode.PreviousNode);
    }
}

    


Calling the Only Nodes Prototype

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XDocument xDocument = new XDocument(
         new XElement("Books",
           new XElement("Book",
            new XAttribute("type", "Author"),
           new XComment("This is a new author."),
            new XElement("FirstName", "A"),
           new XElement("LastName", "B")),
           new XElement("Book",
            new XAttribute("type", "Author"),
           new XElement("FirstName", "C"),
           new XElement("LastName", "D"))));

        IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book");
        foreach (XElement element in elements) {
            Console.WriteLine("Source element: {0} : value = {1}", element.Name, element.Value);
        }
        foreach (XNode node in elements.Nodes()) {
            Console.WriteLine("Child node: {0}", node);
        }
    }
}

    


Calling the Only InDocumentOrder Prototype

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XDocument xDocument = new XDocument(
         new XElement("Books",
           new XElement("Book",
            new XAttribute("type", "Author"),
           new XComment("This is a new author."),
            new XElement("FirstName", "A"),
           new XElement("LastName", "B")),
           new XElement("Book",
            new XAttribute("type", "Author"),
           new XElement("FirstName", "C"),
           new XElement("LastName", "D"))));
        IEnumerable<XNode> nodes = xDocument.Element("Books").Elements("Book").Nodes().Reverse();
        foreach (XNode node in nodes) {
            Console.WriteLine("Source node: {0}", node);
        }
        foreach (XNode node in nodes.InDocumentOrder()) {
            Console.WriteLine("Ordered node: {0}", node);
        }
    }
}