I am going to try my best to keep this post short and concise. I apologize in advance if I need to make a number of edits to add code.
The problem
I have a class I am able to serialize into XML correctly. The class sends the XML to a web service that returns a XML response. The XML response is then deserialized into objects for additional processing. I am able to properly deserialize my objects, providing that the returned XML is not a sequence
What I have tried
I have created my class to look like:
[other class declarations here to support XML response]
public class OrderInfoListResponse
{
public List<OrderInfo> OrderInfo { get; set; }
public OrderInfoListResponse()
{
OrderInfo = new List<OrderInfo>();
}
}
The deserializer looks like:
using (Stream respStream = response.GetResponseStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(OrderListResponse));
orderResp = serializer.Deserialize(respStream) as OrderListResponse;
}
If I pass in data that looks like:
<ResponseBody>
<PageInfo>
<TotalCount>51</TotalCount>
<TotalPageCount>6</TotalPageCount>
<PageSize>10</PageSize>
<PageIndex>1</PageIndex>
</PageInfo>
<RequestID>4546ASDDAS54</RequestID>
<OrderInfoList>
<OrderInfo>
<SellerID>XXXX</SellerID>
<OrderNumber>111111111</OrderNumber>
<InvoiceNumber>222222</InvoiceNumber>
....
</OrderInfo>
<OrderInfoList>
I am able to get the PageInfo data and RequestID with no isses, but my resulting object shows
OrderInfoList
OrderInfo count = 0
If I display the response as a string I get 51 responses.
The question
Why am I not able to deserialize into OrderInfo?
What I’ve tried
I tried the following code, but my count is still 0:
using (Stream respStream = response.GetResponseStream())
{
StreamReader readerOK = new StreamReader(respStream);
string resp = @readerOK.ReadToEnd();
var myEncoder = new ASCIIEncoding();
var bytes = myEncoder.GetBytes(resp);
var memoryStream = new MemoryStream(bytes);
var xmlSerializer = new XmlSerializer(typeof(OrderListResponse));
orderResp = xmlSerializer.Deserialize(memoryStream) as OrderListResponse;
}
Setting a breakpoint on myEncoder shows that string resp contains a full XML response, setting a breakpoint after orderResp shows all fields are set, except for the list objects.
I have instantiated my orderResp object via setting it to null at the start of the method. I have also instantiated every instance of an object contained within that class, both with no results.
If I remove all collections (lists, arrays etc ) from the classes, I am able to deserialize the first response in the XML file, subsequent responses are not deserialized and are skipped.
Any other ideas? Anyone?
The issue was how the class was created.
The original class looked like:
public class OrderListResponseBody
{
The new class looks like: