I recently hit a problem with a SOAP web service where the WSDL specifies an integer for a particular field, but the XML comes in with an empty element which causes the .NET deserialization logic to throw a FormatException.
The WSDL looks like this:
<xsd:element minOccurs="0" maxOccurs="1" name="TransID" type="xsd:int" />
And the SOAP looks like this:
<TransID></TransID>
This causes the exception when parsing the response. The server I’m talking to belongs to a vendor so I don’t have the option to change it. I’m hacking around this in the short term by redefining the field as a string.
My question is this: In trying to diagnose this problem, I pulled out the XML into a file and created my own XmlSerializer to test deserializing using the POCO’s generated by the WSDL import. The XmlSerializer I created parses this XML just fine and produces the parent type as expected. No exception. The field value is zero which would be expected. Why does the SOAP deserializer puke and the plain deserializer I created works fine?
That is normal and expected; a blank string is not valid as an integer, and cannot be parsed.
XmlSerializerbehaves identically: