none
How to generate XML from LINQ query

    Question

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

    Thursday, April 03, 2014 7:27 PM

Answers

  • Hi Mou_kolkata;

    There is no predefined method that will create a XML document from the results of a Linq query. You can construct a document by iterating through the collection of data. For example from the sample you posted you can do something like the following

    // Create a XML document with a root node.
    XDocument xdoc = new XDocument(new XDeclaration("1", "utf8", "no"), 
                                   new XElement("Books"));
    
    // Build an attach all the book nodes
    foreach(var book in booktitles)
    {
        // Create a Book node and its two child nodes
        XElement bookNode = new XElement("Book",
                            new XElement("Name", book.Name),
                            new XElement("Pages", book.Pages));
        // Append the node to the end of the document                    
        xdoc.Root.Add(bookNode); 
    } 
    
    // Save the file out to disk, chaange the file path.
    xdoc.Save("C:\Working Directory\Books.xml");
    
    the results will look something like this.
    <Books>
      <Book>
        <Name>Book 1 Title</Name>
        <Pages>423</Pages>
      </Book>
      <Book>
        <Name>Book 2 Title</Name>
        <Pages>324</Pages>
      </Book>
      <Book>
        <Name>Book 3 Title</Name>
        <Pages>275</Pages>
      </Book>
    </Books>

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, April 04, 2014 3:28 AM

All replies

  • Hi Mou_kolkata;

    There is no predefined method that will create a XML document from the results of a Linq query. You can construct a document by iterating through the collection of data. For example from the sample you posted you can do something like the following

    // Create a XML document with a root node.
    XDocument xdoc = new XDocument(new XDeclaration("1", "utf8", "no"), 
                                   new XElement("Books"));
    
    // Build an attach all the book nodes
    foreach(var book in booktitles)
    {
        // Create a Book node and its two child nodes
        XElement bookNode = new XElement("Book",
                            new XElement("Name", book.Name),
                            new XElement("Pages", book.Pages));
        // Append the node to the end of the document                    
        xdoc.Root.Add(bookNode); 
    } 
    
    // Save the file out to disk, chaange the file path.
    xdoc.Save("C:\Working Directory\Books.xml");
    
    the results will look something like this.
    <Books>
      <Book>
        <Name>Book 1 Title</Name>
        <Pages>423</Pages>
      </Book>
      <Book>
        <Name>Book 2 Title</Name>
        <Pages>324</Pages>
      </Book>
      <Book>
        <Name>Book 3 Title</Name>
        <Pages>275</Pages>
      </Book>
    </Books>

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, April 04, 2014 3:28 AM
  • i got another way which i like.......here it is

    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 ModelCalss { Name = b.Title, Pages = s.pages };
    string myXml = Serialize(booktitles.ToList()).ToString();
       foreach (var title in booktitles)
          lblbooks.Text += String.Format("{0} <br />", title);

    Friday, April 04, 2014 9:05 AM