use Linq to query an XML document : Query « XML LINQ « C# / C Sharp






use Linq to query an XML document

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


static class HelloLinqToXml {
    static void Main() {
        var books = new[] {
      new {Title="A", Publisher="M", Year=2005 },
      new {Title="W", Publisher="M", Year=2006 },
      new {Title="R", Publisher="M", Year=2006 }
    };
        XElement xml = new XElement("books",
          from book in books
          where book.Year == 2006
          select new XElement("book",
            new XAttribute("title", book.Title),
            new XElement("publisher", book.Publisher)
          )
        );
        Console.WriteLine(xml);
    }
}

 








Related examples in the same category

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