文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>光脚丫学LINQ(010):将内存中的对象转换为XML

光脚丫学LINQ(010):将内存中的对象转换为XML

时间:2010-10-29  来源:光脚丫思考

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> 
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载