Mega Code Archive

 
Categories / C# / XML LINQ
 

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