CSharp - XML Query Using LINQ to XML

Description

XML Query Using LINQ to XML

Demo

using System;
using System.Linq;
using System.Xml.Linq;
class Program/*  ww  w .ja v  a 2 s  .com*/
{
    static void Main(string[] args)
    {

        XElement books = XElement.Parse(
          @"<books>  
                <book>  
                    <title>LINQ</title>  
                    <author>Joe</author>  
                </book>  
                <book>  
                    <title>C#</title>  
                    <author>A</author>  
                </book>  
                <book>  
                    <title>VB</title>  
                    <author>B</author>  
                 </book>  
              </books>");

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

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

    }
}

Result

Related Topic