使用XmlSerializer序列化List对象成XML格式(list对象序列化)
程序员文章站
2024-02-23 12:47:04
使用xmlserializer序列化list对象成xml格式序列化成xml格式,和反序列化原格式
复制代码 代码如下:list lst =...
使用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());
}