• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【转载】常见面试题:C#中String和string的区别分析发布时间:2022-07-18
下一篇:
用c++STL实现进程管理发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap