XML转成对象走过的坑

前端之家收集整理的这篇文章主要介绍了XML转成对象走过的坑前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

不想多说,一言不合上代码

待转换的XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response totalCount="1" resultCode="SUCCESS" resultMessage="">
    <profile lastName="李" firstName="*" dateOfBirth="XX0816" age="" gender="1" address="浙江省 杭州富阳市 东州贸有限公" email="slll@163.com" cellEmail="" socialMedia="" homePhone="" cellPhone="199925" contactabiltyMail="1" contactabiltyEmail="1" contactabiltyCellEmail="0" contactabiltyHomePhone="1" contactabiltyCellPhone="1" contactabiltyText="1" contactabiltySocialMedia="0" vipLevel="2" memberId="OC540C00016824" saAccount="660996" class="小姐" homeStoreCode="OC54"/>
    <purchaseHistory>
        <departmentPurchase totalTYAmount="21705" totalTYUnitCnt="9" firstTxn="20160713" lastTxn="20160829" totalLifeTimeSale="36791">
            <department categoryName="D02" tyAmount="8985" tyUnitCnt="4"/>
            <department categoryName="D08" tyAmount="2970" tyUnitCnt="2"/>
            <department categoryName="D01" tyAmount="9750" tyUnitCnt="3"/>
        </departmentPurchase>
        <ratePurchase totalLyAmount="0" totalLyRate="0" totalTyAmount="21705" totalTyRate="100" totalLifeTimeAmount="36791" totalLifeTimeRate="100">
            <rate categoryName="Retail" lyAmount="" lyRate="0" lyToDate="0" tyAmount="21705" tyRate="100" tyToDate="21705" lifeTimeAmount="36791" lifeTimeRate="100" lyTyChange="100"/>
        </ratePurchase>
        <storeRatePurchase>
            <storeRate/>
        </storeRatePurchase>
    </purchaseHistory>
    <vipStatus issueDate="20140629" issueShop="OC54" ptsToUpgrade="21705" expiryDate="20170731" upgradeShop="" ptsToRenew="8130"/>
    <comments comment=""/>
</response>
转换方法
  /// <summary>
        /// 指定XML字符串序列化成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T DeserializeForXml<T>(string xml) where T : class,new()
        {
            System.IO.StringReader stringReader = null;
            try
            {               
                if ((Serializer == null))
                {
                    Serializer = new XmlSerializer(typeof(T));
                }

                stringReader = new StringReader(xml);
                T result = Serializer.Deserialize(stringReader) as T;
                return result;
            }
            finally
            {
                if ((stringReader != null))
                {
                    stringReader.Dispose();
                }
            }
        }
调用方法
rpXML = XMLHelper.DeserializeForXml<CustomerDetails.Response>(returnXML);
T:Response
  /// <summary>
            /// 返回结果(根节点)
            /// </summary>
            [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml","4.0.30319.34209")]
            [System.SerializableAttribute()]
            [System.ComponentModel.DesignerCategoryAttribute("code")]
            [System.Xml.Serialization.XmlRootAttribute(Namespace = "",IsNullable = false,ElementName = "response")]
            public class Response
            {
                /// <summary>
                /// 
                /// </summary>
                [System.Xml.Serialization.XmlAttributeAttribute()]
                public string resultCode { set; get; }

                /// <summary>
                /// 
                /// </summary>
                [System.Xml.Serialization.XmlAttributeAttribute()]
                public string resultMessage { set; get; }

                /// <summary>
                /// 
                /// </summary>
                [System.Xml.Serialization.XmlElementAttribute("profile",typeof(Profile))]
                public Profile profile { set; get; }
              
                        /// <summary>
                /// 
                /// </summary>
                [System.Xml.Serialization.XmlElement("department",typeof(Department),Form = System.Xml.Schema.XmlSchemaForm.Unqualified,Order = 0)]
                public List<Department> department { get; set; }
}
特别注意,如果XML节点有对应的List对象,那么 typeof(Department)一定要这么写,不能是其他的! 原文链接:https://www.f2er.com/xml/294312.html

猜你在找的XML相关文章