首頁 > 軟體

C#複雜XML反序列化為實體物件兩種方式小結

2022-04-02 10:01:24

前言

今天主要講的是如何把通過介面獲取到的Xml資料轉換成(反序列化)我們想要的實體物件,當然Xml反序列化和Json反序列化的方式基本上都是大同小異。都是我們事先定義好對應的對應的Xml實體模型,不過Xml是通過XmlSerializer類的相關特性來對實體物件和 XML檔案之間進行序列化和反序列化操作的。序列化和反序列化其實都還好,我們可以呼叫封裝好的XmlHelper幫助類即可實現,最關鍵的是我們該如何去定義這些實體模型(Model)。當你遇到對方介面一下子返回一大串的Xml資料並且裡面存在很多不同的Xml節點,你該怎麼辦一個一個去解析這些節點到模型上去嗎?本文我主要講兩種方式,第一種方法是通過手寫的方式去定義Xml的實體物件模型類,第二種方法是通過Visual Studio自帶的生成Xml實體物件模型類。

需要操作的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>

一、通過是手寫的方式去定義Xml的實體物件模型類

當然假如你有耐心、時間充足並且眼睛好的話可以使用這種手寫的方式去定義,很多情況寫到最好都會把自己給寫糊塗了(可能是我年紀大了的原因)。

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

    }
}

二、通過Visual Studio自帶的生成Xml實體物件模型類

Vs被稱為宇宙最強IDE也不是沒有理由的,它整合了很多自動建立功能,如自動生成Json類、Xml類等,雖然說使用Vs自動生成的Xml模型可讀性有點差並且有些冗餘,但是快捷省事,只需要略微改動一下即可使用。

1、首先Ctrl+C複製你需要生成的Xml檔案內容

2、找到編輯=》選擇性貼上=》將Xml貼上為類

3、以下是使用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;
            }
        }
    }



}

驗證兩個Xml類是否能夠反序列化成功

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

C# XML基礎入門(XML檔案內容增刪改查清)

https://www.jb51.net/article/243168.htm

C#XmlHelper幫助類操作Xml檔案的通用方法彙總

https://www.jb51.net/article/242389.htm

.NET中XML序列化和反序列化常用類和用來控制XML序列化的屬性總結

https://www.jb51.net/article/243165.htm

到此這篇關於C#複雜XML反序列化為實體物件兩種方式小結的文章就介紹到這了,更多相關C#複雜XML反序列化為實體物件 內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com