0

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);
1
  • You may use Data Contract Serializer to serialize the Query Result and that will be XML. Commented Apr 3, 2014 at 19:36

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

how to store the xml in string variable instead of saving to file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.