C#中linq学习(将泛型集合转换为XML元素)示例

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

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值