Mou
Mou

Reputation: 16282

How to generate XML from LINQ query

we often work with dataset and after populate dataset we can use getxml() function to have the data in xml format. so i like to know is there any similar technique exist for linq to generate xml from linq query. please guide. thanks

IEnumerable<Books> books = Books.GetBooks();
   IEnumerable<Salesdetails> sales = 
                       Salesdetails.getsalesdetails();
   var booktitles = from b in books
             join s in sales
             on b.ID equals s.ID
             select new { Name = b.Title, Pages = s.pages };
   foreach (var title in booktitles)
      lblbooks.Text += String.Format("{0} <br />", title);

Upvotes: 0

Views: 68

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

Yes, here is an example:

var rootElement = new XElement("Books",
                booktitles.Select(
                    x => new XElement("Book",
                        new XElement("Name", x.Name),
                        new XElement("Pages", x.Pages))));

rootElement.Save("path");

You might want to take a look at documentation: Creating XML Trees in C# (LINQ to XML)

Upvotes: 1

Related Questions