A Simple XML Query Using LINQ to XML : Query « XML LINQ « C# / C Sharp






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

 








Related examples in the same category

1.Query XML document with where clause
2.Query XML document by attribute
3.Query XML with Descendants
4.Query XML document with Ancestors and First
5.Get Node by type with OfType
6.Query XML with namespace
7.Use Linq query to get XML document elements
8.Query string array by its element length
9.use Linq to query an XML document