Список <T>не десериализовать XML в C # - PullRequest
1 голос
/ 03 июня 2019

У меня есть xml, и для десериализации я создал класс с типом List<Order>.Но когда я пытаюсь десериализовать его ниже, код не может десериализовать XML, и я не получаю List<Order> значение.

Я предоставляю код, который я пробовал, XML и Class для 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 # Classфайл

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 Ответ

1 голос
/ 03 июня 2019

Попробуйте вместо этого:

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

Важными частями здесь являются атрибуты XmlArray и XmlArrayItem, которые отсутствовали в вашем коде.

...