.net 支持Xml文档与.net 类之间无缝双向转换。当应用程序规模变大后,要想程序变得灵活,可配置元素也会变得越来越多。应用程序根据xml文档配置也就自然而然的事。本文的目的也就讲解xml文档与.net 对象通过xml标签的对应关系,以及xml序列化出现问题后如何解决。
Xml Segment1
<?xml version="1.0" encoding="utf-8" ?> <StudentCollection> </StudentCollection>
对应的映射类:
[XmlRoot("StudentCollection")] public class Student { }
Xml Segement 2
<?xml version="1.0" encoding="utf-8" ?> <customer> </customer>
映射类
[XmlRoot("customer")] public class Customer { }
XmlRoot(“元素名称”):类上的XmlRoot标签指明了类映射到xml的根结点,标签中的名称就是xml文档中根节点的名称。
Xml Segment1
<?xml version="1.0" encoding="utf-8" ?> <student> <age>30</age> </student>
对应的映射类:
[XmlRoot("student")] public class Student { [XmlElement("age",Type=typeof (Int32))] public int Age { get; set; } }
<?xml version="1.0" encoding="utf-8" ?> <employee age="30"> </employee>
Xml 文件Employee.xml
对应的映射类:
[XmlRoot("employee")] public class Employee { [XmlAttribute("age")] public string Age { get; set; } }
Xml 解析:
using (StreamReader sr = new StreamReader("Employee.xml")) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Employee)); Employee stList = xmlSerializer.Deserialize(sr) as Employee; Debug.Assert(stList == null); Console.Read(); }
同时具有XmlElement 与 XmlAttribute
<?xml version="1.0" encoding="utf-8" ?> <student id="1001"> <age>10</age> </student>
[XmlRoot("student")] public class Student { [XmlAttribute("id")] public string ID { get; set; } [XmlElement("age", Type = typeof(Int32))] public int Age { get; set; } } using (StreamReader sr = new StreamReader("Student.xml")) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Student)); Student student = xmlSerializer.Deserialize(sr) as Student; }
<?xml version="1.0" encoding="utf-8" ?> <studentList> <student> <id>1001</id> <age>23</age> <name>hbb0b01</name> <subjects> <subject name="math"> <score> 80 </score> </subject> <subject name="english"> <score> 90 </score> </subject> </subjects> </student> <student> <id>1002</id> <age>30</age> <name>hbb0b02</name> <subjects> <subject name="math"> <score> 70 </score> </subject> <subject name="english"> <score> 60 </score> </subject> </subjects> </student> </studentList>
public class Subject { [XmlAttribute("name")] public string name { get; set; } [XmlElement("score")] public int score { get; set; } } public class Student { public string id { get; set; } public string name { get; set; } public int age { get; set; } /// <summary> /// subjects可以与xml节点的集合节点的名称不同,可以用 /// XmlArray指定映射到的集合节点 /// </summary> [XmlArrayItem(ElementName = "subject", Type = typeof(Subject))] [XmlArray("subjects")] public Subject[] subjects123 { get; set; } } [XmlRoot("studentList")] public class StudentList { [XmlElement(ElementName = "student")] public List<Student> students { get; set; } } using (StreamReader sr = new StreamReader("StudentList.xml")) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(StudentList)); StudentList stList = xmlSerializer.Deserialize(sr) as StudentList; Debug.Assert(stList == null); Console.Read(); }
至此最常用的xml标签XmlRoot,XmlAttribute,XmlElement,XmlArray,XmlArrayItem与类元素的映射关键已经讲完。