XML 反序列化

版权声明:本文为博主原创文章,若转载请标明。 https://blog.csdn.net/qq_36330228/article/details/89280875

XML 反序列化成Model
1.方法:

public static object DeserializeXml(Type type, string xml)
 {
     using (StringReader sr = new StringReader(xml))
     {
         XmlSerializer desXml = new XmlSerializer(type);
         return desXml.Deserialize(sr);
     }
 }
 
//如何调用
DeserializeXml(typeof(Model), xml) as Model;

2.Model中属性与XML节点编注
在反序列化过程中,XML是根据节点将数据放入到Model中,在Model属性对应的XML节点必须要标记清楚,包括XML的命名空间和根节点都要标清,注意XML节点的层级,在Model中也要一一对应,否则可能会反序列化失败。

//根节点 命名空间标记
[XmlRoot(Namespace = "http://xmlSerialization.com/schema/TestDemo", ElementName = "GetResponse", IsNullable = false)]

//节点标记
[XmlElement(ElementName = "GetMyResult")]

3.示例:
  3.1 XML文档

<GetResponse xmlns="http://xmlSerialization.com/schema/TestDemo">
    <GetMyResult>
        <FeesEstimateResultList>
            <FeesEstimateResult>
                <FeesEstimate>
                    <TotalFeesEstimate>
                        <CurrencyCode>RMB</CurrencyCode>
                        <Amount>29.96</Amount>              //需要
                    </TotalFeesEstimate>                                       
                </FeesEstimate>
                <FeesEstimateIdentifier>                 
                    <IdValue>ZZ12345</IdValue>              //需要
                    <PriceToEstimateFees>
                        <ListingPrice>
                            <CurrencyCode>RMB</CurrencyCode>
                            <Amount>499.37</Amount>         //需要
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>RMB</CurrencyCode>
                            <Amount>0</Amount>
                        </Shipping>                       
                    </PriceToEstimateFees>
                </FeesEstimateIdentifier>
                <Status>Success</Status>						//需要
            </FeesEstimateResult>
        </FeesEstimateResultList>
    </GetMyResult>
</GetResponse>

上面备注需要为Model的内容,既需要提取XML中的数据。

  3.2 Model类

    //XML 根 命名空间
    [XmlRoot(Namespace = "http://xmlSerialization.com/schema/TestDemo", ElementName = "GetResponse", IsNullable = false)]
    public class RequestResponse
    {
        //对应XML中节点名称
        [XmlElement(ElementName = "GetMyResult")]
        public GetMyFeesEstimateResult getMyFeesEstimateResult;
    }

    public class GetMyFeesEstimateResult
    {
        [XmlElement(ElementName = "FeesEstimateResultList")]
        public FeesEstimateResultList feesEstimateResultList;       
    }

    public class FeesEstimateResultList
    {
        [XmlElement(ElementName = "FeesEstimateResult")]
        public List<FeesEstimateResult> feesEstimateResult;
    }

    public class FeesEstimateResult
    {
        [XmlElement(ElementName = "FeesEstimate")]
        public FeesEstimate feesEstimate;
        [XmlElement(ElementName = "FeesEstimateIdentifier")]
        public FeesEstimateIdentifier feesEstimateIdentifier;
        [XmlElement(ElementName = "Status")]
        public string Status;                           //需要
    }
    
    public class FeesEstimate
    {
        [XmlElement(ElementName = "TotalFeesEstimate")]
        public TotalFeesEstimate totalFeesEstimate;
    }

    public class TotalFeesEstimate
    {
        [XmlElement(ElementName = "Amount")]
        public decimal Fee;                             //需要
    }

    public class FeesEstimateIdentifier
    {
        [XmlElement(ElementName = "IdValue")]
        public string SKU;                              //需要
        [XmlElement(ElementName = "PriceToEstimateFees")]
        public PriceToEstimateFees priceToEstimateFees;
    }

    public class PriceToEstimateFees
    {
        [XmlElement(ElementName = "ListingPrice")]
        public ListingPrice listingPrice;
    }

    public class ListingPrice
    {
        [XmlElement(ElementName = "Amount")]
        public decimal Price;                           //需要
    }
}

其实需要XML中的数据只有四个,但这个Model却要根据XML层级来写,所以很复杂。在得到反序列化后这个Model后想到里面的数据会一层一层取麻烦,所以在建一个Model如下:

namespace XMLSerializer.Model
{
    public class ResponseModel
    {
        public string Status { get; set; }
        public string SKU { get; set; }
        public decimal Fee { get; set; }
        public decimal Price { get; set; }
        public void ToWriteLineString()
        {
            Console.WriteLine("Status:"+Status+ ",SKU:"+ SKU+ ",Fee:"+ Fee+ ",Price:"+ Price);
        }
    }

  3.3 入口程序

public class Program
    {
        public static void Main(string[] args)
        {
            string xml = "<GetResponse xmlns=\"http://xmlSerialization.com/schema/TestDemo\"><GetMyResult><FeesEstimateResultList><FeesEstimateResult><FeesEstimate><TotalFeesEstimate><CurrencyCode>RMB</CurrencyCode><Amount>29.96</Amount></TotalFeesEstimate></FeesEstimate><FeesEstimateIdentifier><IdValue>ZZ12345</IdValue><PriceToEstimateFees><ListingPrice><CurrencyCode>RMB</CurrencyCode><Amount>499.37</Amount></ListingPrice><Shipping><CurrencyCode>RMB</CurrencyCode><Amount>0</Amount></Shipping></PriceToEstimateFees></FeesEstimateIdentifier><Status>Success</Status></FeesEstimateResult></FeesEstimateResultList></GetMyResult></GetResponse>";         
            RequestResponse RequestResponse= DeserializeXml(typeof(RequestResponse), xml) as RequestResponse;

            //将XML数据放到ResponseModel中
            ResponseModel responseModel = new ResponseModel();
            responseModel.Status = RequestResponse.getMyFeesEstimateResult.feesEstimateResultList.feesEstimateResult[0].Status;
            responseModel.SKU = RequestResponse.getMyFeesEstimateResult.feesEstimateResultList.feesEstimateResult[0].feesEstimateIdentifier.SKU;
            responseModel.Fee = RequestResponse.getMyFeesEstimateResult.feesEstimateResultList.feesEstimateResult[0].feesEstimate.totalFeesEstimate.Fee;
            responseModel.Price = RequestResponse.getMyFeesEstimateResult.feesEstimateResultList.feesEstimateResult[0].feesEstimateIdentifier.priceToEstimateFees.listingPrice.Price;

            responseModel.ToWriteLineString();
            Console.ReadKey();
        }
        
        //反序列化
        public static object DeserializeXml(Type type, string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer desXml = new XmlSerializer(type);
                return desXml.Deserialize(sr);
            }
        }
    }

在这里插入图片描述

没有更多推荐了,返回首页