简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM