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