zoukankan      html  css  js  c++  java
  • 光脚丫学LINQ(010):将内存中的对象转换为XML

    视频演示:http://u.115.com/file/f24db1fdfa

    通过 LINQ 查询,可以轻松地在内存中的数据结构、SQL 数据库、ADO.NET 数据集和 XML 流或文档之间转换数据。下面的示例将内存中的数据结构中的对象转换为 XML 元素。

    List<Student> Students = new List<Student>()   
    {   
        new Student {   
            FirstName="Svetlana",   
            LastName="Omelchenko",    
            ID=111,    
            Scores = new List<int>{97, 92, 81, 60}},   
        new Student {   
            FirstName="Claire",    
            LastName="O’Donnell",    
            ID=112,    
            Scores = new List<int>{75, 84, 91, 39}},   
        new Student {   
            FirstName="Sven",    
            LastName="Mortensen",    
            ID=113,    
            Scores = new List<int>{88, 94, 65, 91}},   
    };   
      
    // Create the query.   
    var StudentsToXML = new XElement("Root",   
        from student in Students   
        let ScoreString = String.Format("{0},{1},{2},{3}",   
            student.Scores[0],   
            student.Scores[1],   
            student.Scores[2],   
            student.Scores[3])   
        select new XElement("Student",   
                   new XElement("FirstName", student.FirstName),   
                   new XElement("LastName", student.LastName),   
                   new XElement("Scores", ScoreString)   
                )    
            );   
      
    // Execute the query.   
    Console.WriteLine(StudentsToXML);   
      
    // Keep the console open in debug mode.   
    Console.WriteLine("Press any key to exit.");   
    Console.ReadKey();  
    List<Student> Students = new List<Student>()
    {
        new Student {
            FirstName="Svetlana",
            LastName="Omelchenko", 
            ID=111, 
            Scores = new List<int>{97, 92, 81, 60}},
        new Student {
            FirstName="Claire", 
            LastName="O’Donnell", 
            ID=112, 
            Scores = new List<int>{75, 84, 91, 39}},
        new Student {
            FirstName="Sven", 
            LastName="Mortensen", 
            ID=113, 
            Scores = new List<int>{88, 94, 65, 91}},
    };
    
    // Create the query.
    var StudentsToXML = new XElement("Root",
        from student in Students
        let ScoreString = String.Format("{0},{1},{2},{3}",
            student.Scores[0],
            student.Scores[1],
            student.Scores[2],
            student.Scores[3])
        select new XElement("Student",
                   new XElement("FirstName", student.FirstName),
                   new XElement("LastName", student.LastName),
                   new XElement("Scores", ScoreString)
                ) 
            );
    
    // Execute the query.
    Console.WriteLine(StudentsToXML);
    
    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
    

    此代码生成下面的 XML 输出:

    <Root>  
      <Student>  
        <FirstName>Svetlana</FirstName>  
        <LastName>Omelchenko</LastName>  
        <Scores>97,92,81,60</Scores>  
      </Student>  
      <Student>  
        <FirstName>Claire</FirstName>  
        <LastName>O’Donnell</LastName>  
        <Scores>75,84,91,39</Scores>  
      </Student>  
      <Student>  
        <FirstName>Sven</FirstName>  
        <LastName>Mortensen</LastName>  
        <Scores>88,94,65,91</Scores>  
      </Student>  
    </Root> 
  • 相关阅读:
    JavaScript 判断 URL
    AppCan 文件上传实例
    IIS6.0手动安装与配置asp.net2.0全过程
    轻松搭建一个Windows SVN服务器
    IIS6.0上某些文件类型不能下载
    JavaScript 数组转字符串,字符串转数组
    百度地图API 应用实例
    IIS7.5 下:HTTP 错误 404.17 Not Found 请求的内容似乎是脚本 解决方法
    SQL 交集 差集 并集 笛卡尔积 应用实例
    Win7 64位 IIS未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项
  • 原文地址:https://www.cnblogs.com/GJYSK/p/1864240.html
Copyright © 2011-2022 走看看