應用XmlSerializer序列化List對象成XML格局(list對象序列化)。本站提示廣大學習愛好者:(應用XmlSerializer序列化List對象成XML格局(list對象序列化))文章只能為提供參考,不一定能成為您想要的結果。以下是應用XmlSerializer序列化List對象成XML格局(list對象序列化)正文
應用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());
}