LINQ生成XML格式数据
| 代码如下 | 复制代码 |
| using System.Xml.Linq; static void Main(string[] args) { //LINQ生成XML格式的数据,相比较把对象序列化成XML文件要灵活性好点using System.Xml.Linq; List list = new List { new Student{ID=1,Name="林书豪",Scores=new List{80,90,100}}, new Student{ID=1,Name="张三丰",Scores=new List{77,88,99}} }; var xml = new XElement("Root", from l in list let x = String.Format("{0},{1},{2}", l.Scores[0], l.Scores[1], l.Scores[2]) select new XElement("Student", new XElement("ID", l.ID), new XElement("Name", l.Name), new XElement("Scores", x) ) ); Console.Write(xml); Console.Read(); /* 1 林书豪 80,90,100 2 张三丰 877,88,99 */ } class Student { public int ID { get; set; } public string Name { get; set; } public List Scores; } |
|
序列化成XML文件
对象序列化成XML
| 代码如下 | 复制代码 |
|
using System.Xml.Serialization; StreamWriter file = new StreamWriter(Server.MapPath("product.xml")); public class Product <?xml version="1.0" encoding="utf-8"?>
苹果 5.5
橘子 2.5
干柿子 16
|
|