List <T>不在C#中反序列化xml

时间:2019-06-03 06:21:22

标签: c# xml deserialization

我有一个xml,为了反序列化,我创建了一个List<Order>类型的类。但是,当我尝试在下面将其反序列化时,代码将无法对XML进行反序列化,并且我无法获得List<Order>的值。

我正在提供我尝试过的代码,XML和XML的类。

代码

XmlSerializer serializer = new XmlSerializer(typeof(ECFindOrderResponse.Envelope));    
                using (TextReader reader = new StringReader(str))
                {
                    if (reader != null)
                    {
                        ECFindOrderResponse.Envelope result = (ECFindOrderResponse.Envelope)serializer.Deserialize(reader);
                        if (result != null && result.Body != null)
                        {

                        }
                    }
                }

我希望将XML作为反序列化的响应

 <?xml version="1.0" encoding="UTF-8"?>
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP:Body>
      <m:FindOrderResponse xmlns:m="http://www.courier.com/schemas/">
         <Orders>
            <Order OrderID="9653653" OrderNumber="9653653" Auth="19204071" Service="NAT-Other" AmountCharged="96.39" PodName="Tina Sample" PodDateTime="10/8/2016 10:19:00 AM" OrderDate="10/8/2016">
               <Stops>
                  <Stop Sequence="1" Name="United Airlines" Address="AWB#016-9672-4071" City="LOUISVILLE" State="KY" Zip="40213" DispatchZone="40213" />
                  <Stop Sequence="2" Name="Tina Sample" Address="6300 Aurora Ave" City="Charlestown" State="IN" Zip="47111" DispatchZone="47111" />
               </Stops>
            </Order>
            <Order OrderID="9653653" OrderNumber="9653653" Auth="19204071" Service="NAT-Other" AmountCharged="96.39" PodName="Tina Sample" PodDateTime="10/8/2016 10:19:00 AM" OrderDate="10/8/2016">
               <Stops>
                  <Stop Sequence="1" Name="United Airlines" Address="AWB#016-9672-4071" City="LOUISVILLE" State="KY" Zip="40213" DispatchZone="40213" />
                  <Stop Sequence="2" Name="Tina Sample" Address="6300 Aurora Ave" City="Charlestown" State="IN" Zip="47111" DispatchZone="47111" />
               </Stops>
            </Order>
         </Orders>
      </m:FindOrderResponse>
   </SOAP:Body>
</SOAP:Envelope> 

C#类文件

public  class ECFindOrderResponse
{
    [XmlRoot(ElementName = "Stop")]
    public class Stop
    {
        [XmlAttribute(AttributeName = "Sequence")]
        public string Sequence { get; set; }
        [XmlAttribute(AttributeName = "Name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName = "Address")]
        public string Address { get; set; }
        [XmlAttribute(AttributeName = "City")]
        public string City { get; set; }
        [XmlAttribute(AttributeName = "State")]
        public string State { get; set; }
        [XmlAttribute(AttributeName = "Zip")]
        public string Zip { get; set; }
        [XmlAttribute(AttributeName = "DispatchZone")]
        public string DispatchZone { get; set; }
    }

    [XmlRoot(ElementName = "Stops")]
    public class Stops
    {
        [XmlElement(ElementName = "Stop")]
        public List<Stop> Stop { get; set; }
    }

    [XmlRoot(ElementName = "Order")]
    public class Order
    {
        [XmlElement(ElementName = "Stops")]
        public Stops Stops { get; set; }
        [XmlAttribute(AttributeName = "OrderID")]
        public string OrderID { get; set; }
        [XmlAttribute(AttributeName = "OrderNumber")]
        public string OrderNumber { get; set; }
        [XmlAttribute(AttributeName = "Auth")]
        public string Auth { get; set; }
        [XmlAttribute(AttributeName = "Service")]
        public string Service { get; set; }
        [XmlAttribute(AttributeName = "AmountCharged")]
        public string AmountCharged { get; set; }
        [XmlAttribute(AttributeName = "PodName")]
        public string PodName { get; set; }
        [XmlAttribute(AttributeName = "PodDateTime")]
        public string PodDateTime { get; set; }
        [XmlAttribute(AttributeName = "OrderDate")]
        public string OrderDate { get; set; }
    }

    [XmlRoot(ElementName = "Orders")]
    public class Orders
    {
        [XmlElement(ElementName = "Order")]
        public List<Order> Order { get; set; }
    }

    [XmlRoot(ElementName = "FindOrderResponse", Namespace = "http://www.courier.com/schemas/")]
    public class FindOrderResponse
    {
        [XmlElement(ElementName = "Orders")]
        public Orders Orders { get; set; }
        [XmlAttribute(AttributeName = "m", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string M { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "FindOrderResponse", Namespace = "http://www.e-courier.com/schemas/")]
        public FindOrderResponse FindOrderResponse { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "SOAP", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string SOAP { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

[XmlRoot(ElementName = "FindOrderResponse", Namespace = "http://www.courier.com/schemas/")]
public class FindOrderResponse
{
    [XmlArray("Orders")]
    [XmlArrayItem("Order")]
    public List<Order> Orders { get; set; }
    [XmlAttribute(AttributeName = "m", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string M { get; set; }
}

这里重要的部分是代码中缺少的XmlArrayXmlArrayItem属性。