Barefoot learning LINQ (010): Converting objects in the memory into XML

Source: Internet
Author: User

Http://u.115.com/file/f24db1fdfa demo

By using LINQ, you can easily convert data between data structures in the memory, SQL databases, ADO. Net datasets, and XML streams or documents. The following example converts an object in the data structure in the memory to an XML element.

List<Student> Students = new List<Student>()   {       new Student {           FirstName="Svetlana",           LastName="Omelchenko",            ID=111,            Scores = new List<int>{97, 92, 81, 60}},       new Student {           FirstName="Claire",            LastName="O’Donnell",            ID=112,            Scores = new List<int>{75, 84, 91, 39}},       new Student {           FirstName="Sven",            LastName="Mortensen",            ID=113,            Scores = new List<int>{88, 94, 65, 91}},   };     // Create the query.   var StudentsToXML = new XElement("Root",       from student in Students       let ScoreString = String.Format("{0},{1},{2},{3}",           student.Scores[0],           student.Scores[1],           student.Scores[2],           student.Scores[3])       select new XElement("Student",                  new XElement("FirstName", student.FirstName),                  new XElement("LastName", student.LastName),                  new XElement("Scores", ScoreString)               )            );     // Execute the query.   Console.WriteLine(StudentsToXML);     // Keep the console open in debug mode.   Console.WriteLine("Press any key to exit.");   Console.ReadKey();  List<Student> Students = new List<Student>(){    new Student {        FirstName="Svetlana",        LastName="Omelchenko",         ID=111,         Scores = new List<int>{97, 92, 81, 60}},    new Student {        FirstName="Claire",         LastName="O’Donnell",         ID=112,         Scores = new List<int>{75, 84, 91, 39}},    new Student {        FirstName="Sven",         LastName="Mortensen",         ID=113,         Scores = new List<int>{88, 94, 65, 91}},};// Create the query.var StudentsToXML = new XElement("Root",    from student in Students    let ScoreString = String.Format("{0},{1},{2},{3}",        student.Scores[0],        student.Scores[1],        student.Scores[2],        student.Scores[3])    select new XElement("Student",               new XElement("FirstName", student.FirstName),               new XElement("LastName", student.LastName),               new XElement("Scores", ScoreString)            )         );// Execute the query.Console.WriteLine(StudentsToXML);// Keep the console open in debug mode.Console.WriteLine("Press any key to exit.");Console.ReadKey();

This code generates the following XML output:

<Root>    <Student>      <FirstName>Svetlana</FirstName>      <LastName>Omelchenko</LastName>      <Scores>97,92,81,60</Scores>    </Student>    <Student>      <FirstName>Claire</FirstName>      <LastName>O’Donnell</LastName>      <Scores>75,84,91,39</Scores>    </Student>    <Student>      <FirstName>Sven</FirstName>      <LastName>Mortensen</LastName>      <Scores>88,94,65,91</Scores>    </Student>  </Root> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.