Create new XML element in LINQ in CSharp
Description
The following code shows how to create new XML element in LINQ.
Example
/*w w w . ja v a 2 s. co m*/
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);
}
}