使用XmlSerializer序列化List对象成XML格式(list对象序列化)
发布时间:2016-12-28 来源:查字典编辑
摘要:使用XmlSerializer序列化List对象成XML格式序列化成XML格式,和反序列化原格式复制代码代码如下:Listlst=newLi...
使用XmlSerializer序列化List对象成XML格式
序列化成XML格式,和反序列化原格式
复制代码 代码如下:
List<Customer> lst = new List<Customer>();
Customer c = new Customer();
c.ID = 1;
c.Name = "one";
lst.Add(c);
c = new Customer();
c.ID = 2;
c.Name = "tow";
lst.Add(c);
XmlDocument xd = new XmlDocument();
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(lst.GetType());
xz.Serialize(sw, lst);
Console.WriteLine(sw.ToString());
xd.LoadXml(sw.ToString());
xd.Save("c:1.xml");
}
List<Customer> lst2 = new List<Customer>();
using (XmlReader reader = XmlReader.Create("c:1.xml"))
{
XmlSerializer xz = new XmlSerializer(lst2.GetType());
lst2=(List<Customer>)xz.Deserialize(reader);
Console.WriteLine(reader.ToString());
}