Query XML document by attribute

   
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;

class Program {
    static void Main(string[] args) {
        XElement xml = XElement.Load("Employee.xml");
        var query = from s in xml.Elements("salary").Elements("idperson")
                    where (int)s.Attribute("year") == 2004
                    select s;

        foreach (var record in query) {
            Console.WriteLine("Amount: {0}", (string)record.Attribute("salaryyear"));
        }
    }
}

    


Query XML document with where clause

   
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;

class Program {
    static void Main(string[] args) {
        XElement xml = XElement.Load(@"Employee.xml");
        var query = from p in xml.Elements("person")
                    where (int)p.Element("id") == 1
                    select p;

        foreach (var record in query) {
            Console.WriteLine("Employee: {0} {1}",
                                                record.Element("firstname"),
                                                record.Element("lastname"));
        }
    }
}

    


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);
    }
}