1

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!

Peter
  • 217
  • 1
  • 2
  • 8
  • If I run your code with 10000 items in the list I can't reproduce. If I do `XDocument.Parse(invoices);` it doesn't bark at me. Where and how do you notice that the output is wrong? I'm running this on .Net 4 +, are you on something older? – rene Nov 02 '16 at 10:42
  • Unf*cking believable. Because of your comment I double checked and came to this answer: http://stackoverflow.com/questions/34054126/visual-studio-text-visualizer-missing-text And yes, I use Update 1. It's the text visualizer, not the actual string. So the bug in my code I was investigating was not due to this issue. Thanks! – Peter Nov 02 '16 at 11:38

0 Answers0