Search code examples
c#xmlopayo

Deserializing XML - Value was either too large or too small for an unsigned byte


I am downloading transaction data from SagePay using their API. The result from the call gives me an XML string which I then deserialize and store in a class.

Each call retrieves the maximum 50 records, I make multiple calls a given datetime span until all records have been retrieved. This works fine for the first X calls but then I get an error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll

Additional information: There is an error in XML document (1, 141).

Inner Exception: {"Value was either too large or too small for an unsigned byte."}

The values in XML document (1, 141). do not always remain the same with each try. If I look at the character at this position I can see nothing wrong.

In this example character 141 is the "t" in <totalrows>:

<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><vspaccess><errorcode>0000</errorcode><transactions><startrow>251</startrow><endrow>300</endrow><totalrows>16684</totalrows>

The <startrow> of 251 shows that 5 successful calls have gone before this one that's errored.

Deserialize code:

XmlSerializer serializer = new XmlSerializer(typeof(vspaccess));
StringReader rdr = new StringReader(xmlDoc.InnerXml);
vspaccess sageTransactions = (vspaccess)serializer.Deserialize(rdr);

vspaccess is the class generated by VS2013 (paste special)

What am I missing?

I obviously cannot post all the XML as it is live transactional info but if I have missed any required info please let me know


Solution

  • read the error. unsigned byte max size is 255 -- so a value of 300 would be to big.

    16684 would also be to big. don't get hung up on the column number the error reports. look at the error message and what you are loading.