光脚丫学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> 

转载于:https://www.cnblogs.com/GJYSK/archive/2010/10/29/1864240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值