Reputation: 3494
I have a xml and for deserialize it I created a class with List<Order>
type. But when i try to deserialize it below code is not able to deserialize the XML and i am not getting List<Order>
value.
I am providing the code i have tried, XML and Class for XML.
Code
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)
{
}
}
}
My expected XML as a response that i want to deserialize
<?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 file
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; }
}
}
Upvotes: 1
Views: 96
Reputation: 174309
Try this instead:
[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; }
}
The important parts here are the XmlArray
and XmlArrayItem
attributes which were missing in your code.
Upvotes: 1