日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Xml Document与 xml反序列化

發布時間:2025/3/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Xml Document与 xml反序列化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概念

.net 支持Xml文檔與.net 類之間無縫雙向轉換。當應用程序規模變大后,要想程序變得靈活,可配置元素也會變得越來越多。應用程序根據xml文檔配置也就自然而然的事。本文的目的也就講解xml文檔與.net 對象通過xml標簽的對應關系,以及xml序列化出現問題后如何解決。

基本的序列化元素

[XmlRoot(“ElementName”)]:對應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文檔中根節點的名稱。

[XmlElement(“ElementName”)]:映射到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>

?

[XmlAttribute(“AttributeName”)]:映射到xml文檔中屬性。

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;}

?

?

?

XmlAttayItem 與 XmlElement 以及XmlArray節點

<?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與類元素的映射關鍵已經講完。

轉載于:https://www.cnblogs.com/hbb0b0/archive/2013/01/30/2883683.html

總結

以上是生活随笔為你收集整理的Xml Document与 xml反序列化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。