C # examples of how to convert a generic set to an XML Element

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Xml. Linq;

Namespace LinqStudy
{
//************************************** **************************************** *****************************
// Module: Program. cs
// Author: limeteor
// Create Date: 2008-06-27
//************************************** **************************************** *****************************
Class Program
{
Static void Main (string [] args)
{
List <Student> students = new List <Student> (){

New Student {First = "Svetlana", Last = "Omelchenko", ID = 111, Street = "123 Main Street", City = "Seattle ", scores = new List <int> () {97, 92, 81, 60 }},
New Student {First = "Clare", Last = "O 'Donnell", ID = 112, Street = "124 Main Street", City = "Redmond ", scores = new List <int> () {75, 84, 91, 39 }},
New Student {First = "Sven", Last = "Mortensen", ID = 113, Street = "125 Main Street", City = "Lake City ", scores = new List <int> {88, 94, 65, 91 }},
};

List <Teacher> teachers = new List <Teacher> (){

New Teacher {First = "Ann", Last = "Beebe", ID = 945, City = "Seattle "},
New Teacher {First = "Alex", Last = "Robinson", ID = 956, City = "Redmond "},
New Teacher {First = "Michiyo", Last = "Sato", ID = 972, City = "Tacoma "}
};

// Generate xml format using query syntax
Var studentXML = new XElement ("Root", from student in students
Let x = String. format ("{0}, {1}, {2}, {3}", student. scores [0], student. scores [1], student. scores [2], student. scores [3])
Select new XElement ("student ",
New XElement ("First", student. First ),
New XElement ("Last", student. Last ),
New XElement ("Scores", x )));

// Use Lambda expressions (method syntax) to generate xml format
Var teacherXML = new XElement ("Root", teachers. Select (p => new XElement ("Teacher", new XElement ("First", p. First ),
New XElement ("Last", p. Last ),
New XElement ("Id", p. ID ),
New XElement ("City", p. City ))));

Console. WriteLine ("--------- start printing ----------------------");
Console. WriteLine (studentXML );
Console. WriteLine ("----------------------------------------");
Console. WriteLine (teacherXML );
Console. WriteLine ("----------------------------------------");
Foreach (var I in teacherXML. Nodes ())
{
Console. WriteLine (I); // print the set of child nodes of the first node
}
Console. WriteLine ("-------------- print finished ------------------");
Console. ReadKey ();

}
}
//************************************** **************************************** *****************************
// Module: Student. cs
// Author: limeteor
// Create Date: 2008-06-27
//************************************** **************************************** *****************************
Class Student
{
Public string First {get; set ;}
Public string Last {get; set ;}
Public int ID {get; set ;}
Public string Street {get; set ;}
Public string City {get; set ;}
Public List <int> Scores;
}
//************************************** **************************************** *****************************
// Module: Teacher. cs
// Author: limeteor
// Create Date: 2008-06-27
//************************************** **************************************** *****************************
Class Teacher
{
Public string First {get; set ;}
Public string Last {get; set ;}
Public int ID {get; set ;}
Public string City {get; set ;}

}
}

Related Article

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.