C# XML 的序列化和反序列化
原创
©著作权归作者所有:来自51CTO博客作者xxj_jing的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.xml 集合序列化和发序列化
1.1 对象结构
public class People
{
[XmlAttribute("NAME")]
public string Name
{ set; get; }
[XmlAttribute("AGE")]
public int Age
{ set; get; }
}
[XmlRoot("Root")]
public class Student : People
{
[XmlElement("CLASS")]
public string Class
{ set; get; }
[XmlElement("NUMBER")]
public int Number
{ set; get; }
}
2.对象序列化为 xml 文件
public void ToXML()
{ List<Student> stuList = new List<Student>();
stuList.Add(new Student() { Age = 10, Number = 1, Name = "Tom", Class = "Class One" });
stuList.Add(new Student() { Age = 11, Number = 2, Name = "Jay", Class = "Class Two" });
stuList.Add(new Student() { Age = 12, Number = 3, Name = "Pet", Class = "Class One" });
stuList.Add(new Student() { Age = 13, Number = 4, Name = "May", Class = "Class Three" });
stuList.Add(new Student() { Age = 14, Number = 5, Name = "Soy", Class = "Class Two" });
XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
ser.Serialize(File.Create("C:\\x.xml"), stuList); }
3.序列化后 xml 内容
<?xml version="1.0"?>
<ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Student NAME="Tom" AGE="10">
<CLASS>Class One</CLASS>
<NUMBER>1</NUMBER>
</Student>
<Student NAME="Jay" AGE="11">
<CLASS>Class Two</CLASS>
<NUMBER>2</NUMBER>
</Student>
<Student NAME="Pet" AGE="12">
<CLASS>Class One</CLASS>
<NUMBER>3</NUMBER>
</Student>
<Student NAME="May" AGE="13">
<CLASS>Class Three</CLASS>
<NUMBER>4</NUMBER>
</Student>
<Student NAME="Soy" AGE="14">
<CLASS>Class Two</CLASS>
<NUMBER>5</NUMBER>
</Student>
</ArrayOfStudent>
4.将 xml 反序列化为对象
public List<Student> ToStudent()
{
XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
List<Student> stuList = ser.Deserialize(File.OpenRead("C:\\x.xml")) as List<Student>; return stuList;
}
5.测试脚本
5.1测试类
public class Test
{
public class People
{
[XmlAttribute("NAME")]
public string Name
{ set; get; }
[XmlAttribute("AGE")]
public int Age
{ set; get; }
}
[XmlRoot("Root")]
public class Student : People
{
[XmlElement("CLASS")]
public string Class
{ set; get; }
[XmlElement("NUMBER")]
public int Number
{ set; get; }
}
public void ToXML()
{ List<Student> stuList = new List<Student>();
stuList.Add(new Student() { Age = 10, Number = 1, Name = "Tom", Class = "Class One" });
stuList.Add(new Student() { Age = 11, Number = 2, Name = "Jay", Class = "Class Two" });
stuList.Add(new Student() { Age = 12, Number = 3, Name = "Pet", Class = "Class One" });
stuList.Add(new Student() { Age = 13, Number = 4, Name = "May", Class = "Class Three" });
stuList.Add(new Student() { Age = 14, Number = 5, Name = "Soy", Class = "Class Two" });
XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
ser.Serialize(File.Create("C:\\x.xml"), stuList); }
public List<Student> ToStudent()
{
XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
List<Student> stuList = ser.Deserialize(File.OpenRead("C:\\x.xml")) as List<Student>; return stuList;
} }
5.2测试脚本
private void btnOk_Click(object sender, EventArgs e)
{
Test t = new Test();
t.ToXML();
t.ToStudent();
}
注意:xml 的存储结构类似 C# 中的继承,所以如果节点有嵌套,需要考虑是否需要用到类继承的数据结构。
参考:http://apps.hi.baidu.com/share/detail/137402