C#中的xml序列化过程(详细的例子)?
展开全部
要序列化的对象:
[Serializable]
public class Person
{
public string Name{get;set;}
public int Age{get;set;}
}
实例化一个对象:
Person p = new Person();
p.Name="张三";
p.Age=18;
序列化:
Type typePer = p.GetType();
XmlSerializer xmlSer = new XmlSerializer(typePer);
using (FileStream fs = new FileStream("person.xml", FileMode.Create))
{
xmlSer.Serialize(fs, p);
}
也可以序列化一个LIST集合:
List lps = new List();
lps.Add(对象);
lps.Add(对象);
......
XmlSerializer x = new XmlSerializer(lps.GetType());
using (FileStream fs = new FileStream(@"D:\per.xml", FileMode.Create, FileAccess.Write))
{
x.Serialize(fs, lps);
}
[Serializable]
public class Person
{
public string Name{get;set;}
public int Age{get;set;}
}
实例化一个对象:
Person p = new Person();
p.Name="张三";
p.Age=18;
序列化:
Type typePer = p.GetType();
XmlSerializer xmlSer = new XmlSerializer(typePer);
using (FileStream fs = new FileStream("person.xml", FileMode.Create))
{
xmlSer.Serialize(fs, p);
}
也可以序列化一个LIST集合:
List lps = new List();
lps.Add(对象);
lps.Add(对象);
......
XmlSerializer x = new XmlSerializer(lps.GetType());
using (FileStream fs = new FileStream(@"D:\per.xml", FileMode.Create, FileAccess.Write))
{
x.Serialize(fs, lps);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询