C# LINQ to XML示例
2013-11-09 15:16
253 查看
static void Main(string[] args)
{ // Create the data source by using a collection initializer.
var students = new[]
{
new {First="Svetlana", Last="Omelchenko", ID=111, Scores = new List<int>{97, 92, 81, 60}},
new {First="Claire", Last="O’Donnell", ID=112, Scores = new List<int>{75, 84, 91, 39}},
new {First="Sven", Last="Mortensen", ID=113, Scores = new List<int>{88, 94, 65, 91}},
};
// Create the query.
var studentsToXML = 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)
) // end "student"
); // end "Root"
// Execute the query.
Console.WriteLine(studentsToXML);
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
运行经结果:
{ // Create the data source by using a collection initializer.
var students = new[]
{
new {First="Svetlana", Last="Omelchenko", ID=111, Scores = new List<int>{97, 92, 81, 60}},
new {First="Claire", Last="O’Donnell", ID=112, Scores = new List<int>{75, 84, 91, 39}},
new {First="Sven", Last="Mortensen", ID=113, Scores = new List<int>{88, 94, 65, 91}},
};
// Create the query.
var studentsToXML = 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)
) // end "student"
); // end "Root"
// Execute the query.
Console.WriteLine(studentsToXML);
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
运行经结果:
相关文章推荐
- 参考文章-C# Linq To XML的学习(创建并编辑XML树)示例
- C# Linq To XML的学习(创建并编辑XML树)示例
- C#新手入门代码 LINQ TO XML xattribute的用法示例
- C# Linq To XML的学习(创建并编辑XML树)示例
- C#中linq学习(将泛型集合转换为XML元素)示例
- C# 2008 学习笔记 - LINQ to XML
- C#示例学习(八)-Linq to object(5)分页效果
- C#中的Linq to Xml详解
- C# LINQ to XML
- C#中的Linq to Xml详解
- c# linq to xml 获取某节点属性总数
- C#中的Linq to Xml
- xml to linq C#实例 类和xml文件互转换
- C# LINQ to XML应用介绍
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)
- 【原创】C# Linq to XML
- C#中linq学习(将泛型集合转换为XML元素)示例
- C# 2008 学习笔记 - LINQ to XML
- C# 2008 学习笔记 - LINQ to XML
- C# Linq To XML 读取XML,修改属性