I have a very annoying issue serializing a large XML object. I have defined a class FetchYearlyInvoiceStatusRequest:
[Serializable]
public class FetchYearlyInvoiceStatusRequest
{
[XmlArray("INVOICES")]
[XmlArrayItem("INVOICE", typeof(InvoiceRequest))]
public List<InvoiceRequest> Invoices;
}
[Serializable]
public class InvoiceRequest
{
[XmlElement("CONTRACTACCOUNT")]
public string ContractAccount;
[XmlElement("INVOICENUMBER")]
public string InvoiceNumber;
}
I have an instance of this class with around 700 items in the list. When I use this code to serialize this to XML:
XmlSerializer serializerRequest = new XmlSerializer(typeof(FetchYearlyInvoiceStatusRequest));
string invoices;
using (var sw = new StringWriter())
{
serializerRequest.Serialize(sw, odsrequest);
invoices = sw.ToString();
}
The invoices string now contains a long list of invoices, but the serializer (or the memorystream?) just cut off the middle half. So really in the middle of the output string, the literal text is:
<INVOICE>
<CONTRACTACCOUNT>3006698362</CONTRACTACCOUNT>
<INVOICENUMBER>40523461958</INVOICENUMBER>
</INVOICE>
<INVOICE>
<CONTRACTACCOUNT>3006362096</CONTRACTACCOUNT>
<INVOICENUMBER>40028149026</INVOICENUMBE... <CONTRACTACCOUNT>3006362096</CONTRACTACCOUNT>
<INVOICENUMBER>55002448279</INVOICENUMBER>
</INVOICE>
<INVOICE>
<CONTRACTACCOUNT>3006362096</CONTRACTACCOUNT>
<INVOICENUMBER>42514938204</INVOICENUMBER>
</INVOICE>
This is simply corrupt. When I write to a file using almost the same code (but using a StreamWriter isntead of StringWriter):
XmlSerializer serializerRequest = new XmlSerializer(typeof(FetchYearlyInvoiceStatusRequest));
string invoices;
using (var sw = new StreamWriter("c:\\temp\\output.xml"))
{
serializerRequest.Serialize(sw, odsrequest);
}
The output in the file is perfect (snippet at the same location as above where it cuts off):
<INVOICE>
<CONTRACTACCOUNT>3006362096</CONTRACTACCOUNT>
<INVOICENUMBER>40028149026</INVOICENUMBER>
</INVOICE>
<INVOICE>
<CONTRACTACCOUNT>3007728722</CONTRACTACCOUNT>
<INVOICENUMBER>40027928855</INVOICENUMBER>
</INVOICE>
Please someone tell me what happens. I've googled for "stringwriter limitations", "xmlserializer limitations and more. Nothing :(
Any help appreciated!