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="Claire",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"}
};
//用查询语法生成xml格式
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)));
//用Lambda表达式(方法语法)生成xml格式
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("---------开始打印----------------------");
Console.WriteLine(studentXML);
Console.WriteLine("----------------------------------------");
Console.WriteLine(teacherXML);
Console.WriteLine("----------------------------------------");
foreach (var i in teacherXML.Nodes())
{
Console.WriteLine(i);//打印第一个节点子节点的集合
}
Console.WriteLine("--------------打印完成------------------");
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; }
}
}
请发表评论