• c# LINQ 将数据集转换成XML


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

    namespace mytest
    {

        public 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();

            }

        }

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

        }

    }

  • 相关阅读:
    编译Linux 2.6内核
    C语言学习参考(基础&进阶)
    用户体验为什么如此重要
    北京讲座:软件企业常见问题和系统性解决方法(7月5日)
    《JavaScript权威指南(第6版)》诚征广大读者参与初译稿审校活动!
    有关正则表达式的研究
    关于分页的研究
    四舍五入等一些不常用代码整理
    最简单CSS实现Table细线表格
    vs2005 SP1补丁安装慢
  • 原文地址:https://www.cnblogs.com/niaowo/p/2167740.html
Copyright © 2020-2023  润新知