<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
今天主要講的是如何把通過介面獲取到的Xml資料轉換成(反序列化)我們想要的實體物件,當然Xml反序列化和Json反序列化的方式基本上都是大同小異。都是我們事先定義好對應的對應的Xml實體模型,不過Xml是通過XmlSerializer類的相關特性來對實體物件和 XML檔案之間進行序列化和反序列化操作的。序列化和反序列化其實都還好,我們可以呼叫封裝好的XmlHelper幫助類即可實現,最關鍵的是我們該如何去定義這些實體模型(Model)。當你遇到對方介面一下子返回一大串的Xml資料並且裡面存在很多不同的Xml節點,你該怎麼辦一個一個去解析這些節點到模型上去嗎?本文我主要講兩種方式,第一種方法是通過手寫的方式去定義Xml的實體物件模型類,第二種方法是通過Visual Studio自帶的生成Xml實體物件模型類。
注意:以下是我稍微簡化的Xml資料,實際資料可能比這裡要複雜個大幾倍。
<?xml version="1.0" encoding="utf-8" ?> <envelope> <header> <version port="1111" host="www.baidu.com">successfuly</version> <timestamp>20211216081218</timestamp> </header> <response type="cities" product="hotel"> <cities> <city> <code value="zg" /> <city_tax value="true" /> <names> <name language="fr" value="ABANO TERME - PADOUE" /> <name language="en" value="ABANO TERME - PADUE" /> <name language="nl" value="ABANO TERME - PADUE" /> </names> </city> <city> <code value="hk" /> <city_tax value="false" /> <names> <name language="fr" value="ABBADIA SAN SALVATORE - SIENNE" /> <name language="en" value="ABBADIA SAN SALVATORE - SIENA" /> <name language="nl" value="ABBADIA SAN SALVATORE - SIENA" /> </names> </city> </cities> </response> </envelope>
當然假如你有耐心、時間充足並且眼睛好的話可以使用這種手寫的方式去定義,很多情況寫到最好都會把自己給寫糊塗了(可能是我年紀大了的原因)。
namespace Practices.Models { /// <summary> /// Envelope /// </summary> [XmlType(TypeName = "envelope")] public class CityDataModel { /// <summary> /// header /// </summary> [XmlElement("header")] public Header header { get; set; } /// <summary> /// response /// </summary> [XmlElement("response")] public Response response { get; set; } } /// <summary> /// Header /// </summary> [XmlType(TypeName = "header")] public class Header { /// <summary> /// version /// </summary> [XmlElement("version")] public Version version { get; set; } /// <summary> /// timestamp /// </summary> [XmlElement("timestamp")] public string timestamp { get; set; } } /// <summary> /// Version /// </summary> public class Version { /// <summary> /// port /// </summary> [XmlAttribute("port")] public string port { get; set; } /// <summary> /// host /// </summary> [XmlAttribute("host")] public string host { get; set; } /// <summary> /// value:XmlTextAttribute指示該屬性作為XML文書處理 /// </summary> [XmlTextAttribute()] public string value { get; set; } } /// <summary> /// Response /// </summary> [XmlType(TypeName = "response")] public class Response { /// <summary> /// type /// </summary> [XmlAttribute] public string type { get; set; } /// <summary> /// product /// </summary> [XmlAttribute] public string product { get; set; } /// <summary> /// cities /// </summary> [XmlArray("cities")] public List<City> cities { get; set; } } /// <summary> /// class: City /// </summary> [XmlType(TypeName = "city")] public class City { /// <summary> /// code /// </summary> [XmlElement("code")] public Code code { get; set; } /// <summary> /// city_tax /// </summary> [XmlElement("city_tax")] public City_tax city_tax { get; set; } /// <summary> /// names /// </summary> [XmlArray("names")] public List<Name> names { get; set; } } /// <summary> /// class: Code /// </summary> [XmlType(TypeName = "code")] public class Code { /// <summary> /// /// </summary> [XmlAttribute("value")] public string value { get; set; } } /// <summary> /// class: City_tax /// </summary> [XmlType(TypeName = "city_tax")] public class City_tax { /// <summary> /// /// </summary> [XmlAttribute("value")] public string value { get; set; } } /// <summary> /// class: Name /// </summary> [XmlType(TypeName = "name")] public class Name { /// <summary> /// /// </summary> [XmlAttribute("language")] public string language { get; set; } /// <summary> /// /// </summary> [XmlAttribute("value")] public string value { get; set; } } }
Vs被稱為宇宙最強IDE也不是沒有理由的,它整合了很多自動建立功能,如自動生成Json類、Xml類等,雖然說使用Vs自動生成的Xml模型可讀性有點差並且有些冗餘,但是快捷省事,只需要略微改動一下即可使用。
namespace Practices.Models { // 注意: 生成的程式碼可能至少需要 .NET Framework 4.5 或 .NET Core/Standard 2.0。 /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] //TODO:注意這裡因為我把類名改成了我自定義的,所以在TypeName這裡需要宣告Xml檔案的節點名 [System.Xml.Serialization.XmlTypeAttribute(typeName: "envelope")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] public partial class NewCityDataModel { private envelopeHeader headerField; private envelopeResponse responseField; /// <remarks/> public envelopeHeader header { get { return this.headerField; } set { this.headerField = value; } } /// <remarks/> public envelopeResponse response { get { return this.responseField; } set { this.responseField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class envelopeHeader { private envelopeHeaderVersion versionField; private ulong timestampField; /// <remarks/> public envelopeHeaderVersion version { get { return this.versionField; } set { this.versionField = value; } } /// <remarks/> public ulong timestamp { get { return this.timestampField; } set { this.timestampField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class envelopeHeaderVersion { private ushort portField; private string hostField; private string valueField; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public ushort port { get { return this.portField; } set { this.portField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string host { get { return this.hostField; } set { this.hostField = value; } } /// <remarks/> [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class envelopeResponse { private envelopeResponseCity[] citiesField; private string typeField; private string productField; /// <remarks/> [System.Xml.Serialization.XmlArrayItemAttribute("city", IsNullable = false)] public envelopeResponseCity[] cities { get { return this.citiesField; } set { this.citiesField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string type { get { return this.typeField; } set { this.typeField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string product { get { return this.productField; } set { this.productField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class envelopeResponseCity { private envelopeResponseCityCode codeField; private envelopeResponseCityCity_tax city_taxField; private envelopeResponseCityName[] namesField; /// <remarks/> public envelopeResponseCityCode code { get { return this.codeField; } set { this.codeField = value; } } /// <remarks/> public envelopeResponseCityCity_tax city_tax { get { return this.city_taxField; } set { this.city_taxField = value; } } /// <remarks/> [System.Xml.Serialization.XmlArrayItemAttribute("name", IsNullable = false)] public envelopeResponseCityName[] names { get { return this.namesField; } set { this.namesField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class envelopeResponseCityCode { private string valueField; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string value { get { return this.valueField; } set { this.valueField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class envelopeResponseCityCity_tax { private bool valueField; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public bool value { get { return this.valueField; } set { this.valueField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class envelopeResponseCityName { private string languageField; private string valueField; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string language { get { return this.languageField; } set { this.languageField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string value { get { return this.valueField; } set { this.valueField = value; } } } }
/// <summary> /// 讀取Xml檔案內容反序列化為指定的物件 /// </summary> /// <param name="filePath">Xml檔案的位置(絕對路徑)</param> /// <returns></returns> public static T DeserializeFromXml<T>(string filePath) { try { if (!File.Exists(filePath)) throw new ArgumentNullException(filePath + " not Exists"); using (StreamReader reader = new StreamReader(filePath)) { XmlSerializer xs = new XmlSerializer(typeof(T)); T ret = (T)xs.Deserialize(reader); return ret; } } catch (Exception ex) { return default(T); } }
https://www.jb51.net/article/243168.htm
https://www.jb51.net/article/242389.htm
https://www.jb51.net/article/243165.htm
到此這篇關於C#複雜XML反序列化為實體物件兩種方式小結的文章就介紹到這了,更多相關C#複雜XML反序列化為實體物件 內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45