Xml Document与 xml反序列化
生活随笔
收集整理的這篇文章主要介紹了
Xml Document与 xml反序列化
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
概念
.net 支持Xml文檔與.net 類之間無縫雙向轉(zhuǎn)換。當(dāng)應(yīng)用程序規(guī)模變大后,要想程序變得靈活,可配置元素也會變得越來越多。應(yīng)用程序根據(jù)xml文檔配置也就自然而然的事。本文的目的也就講解xml文檔與.net 對象通過xml標簽的對應(yīng)關(guān)系,以及xml序列化出現(xiàn)問題后如何解決。
基本的序列化元素
[XmlRoot(“ElementName”)]:對應(yīng)xml文檔的根元素.
Xml Segment1
<?xml version="1.0" encoding="utf-8" ?><StudentCollection></StudentCollection>?
對應(yīng)的映射類:
[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的根結(jié)點,標簽中的名稱就是xml文檔中根節(jié)點的名稱。
[XmlElement(“ElementName”)]:映射到xml文檔中元素。
Xml Segment1
<?xml version="1.0" encoding="utf-8" ?><student><age>30</age></student>?
?
對應(yīng)的映射類:
[XmlRoot("student")]public class Student{[XmlElement("age",Type=typeof (Int32))]public int Age{get;set;}}
?
?
[XmlAttribute(“AttributeName”)]:映射到xml文檔中屬性。
Xml 文件Employee.xml
?
對應(yīng)的映射類:
[XmlRoot("employee")]public class Employee{[XmlAttribute("age")]public string Age{get;set;}}?
Xml 解析:
?
同時具有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;}?
?
?
XmlAttayItem 與 XmlElement 以及XmlArray節(jié)點
<?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節(jié)點的集合節(jié)點的名稱不同,可以用/// XmlArray指定映射到的集合節(jié)點/// </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();}?
總結(jié):
至此最常用的xml標簽XmlRoot,XmlAttribute,XmlElement,XmlArray,XmlArrayItem與類元素的映射關(guān)鍵已經(jīng)講完。
轉(zhuǎn)載于:https://www.cnblogs.com/hbb0b0/archive/2013/01/30/2883683.html
總結(jié)
以上是生活随笔為你收集整理的Xml Document与 xml反序列化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 编程下通知的发送
- 下一篇: Mercurial 版本控制服务器(We