使用XmlSerializer序列化List对象成XML格式(list对象序列化)

使用XmlSerializer序列化List对象成XML格式
序列化成XML格式,和反序列化原格式

复制代码 代码如下:

List lst = new List();
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 lst2 = new List();
using (XmlReader reader = XmlReader.Create("c:\\1.xml"))
{
    XmlSerializer xz = new XmlSerializer(lst2.GetType());
    lst2=(List)xz.Deserialize(reader);
    Console.WriteLine(reader.ToString());
}

你可能感兴趣的:(使用XmlSerializer序列化List对象成XML格式(list对象序列化))