首页 > 编程 > C# > 正文

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

2020-01-24 02:44:14
字体:
来源:转载
供稿:网友

使用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());
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表