LINQ生成XML格式数据
代码如下 | 复制代码 |
using System.Xml.Linq; static void Main(string[] args) { //LINQ生成XML格式的数据,相比较把对象序列化成XML文件要灵活性好点using System.Xml.Linq; List { new Student{ID=1,Name="林书豪",Scores=new List new Student{ID=1,Name="张三丰",Scores=new List }; 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(); /* */ } class Student { public int ID { get; set; } public string Name { get; set; } public List } |
序列化成XML文件
对象序列化成XML
代码如下 | 复制代码 |
using System.Xml.Serialization; StreamWriter file = new StreamWriter(Server.MapPath("product.xml")); public class Product
|