使用XmlSerializer序列化List对象成XML格式
序列化成XML格式,和反序列化原格式
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());
}
新闻热点
疑难解答