Nillable value types for web services
Today I was working on a web service that persist a large XML structure sent by an InfoPath form. I found an interesting issue. The XML schema used by InfoPath declares an XML element as following:
<xs:element name="ShippingDate" type="xs:dateTime" nillable="true"/>
That means that the XML stream can be both validated as:
<ShippingDate>2003-12-17T09:30:47-05:00</ShippingDate>
or
<ShippingDate xsi:nil="true" />
The problem rises when you send the second one to the web method. You'll get a SoapException such as "String was not recognized as a valid DateTime." Ough!! Yes, the DateTime is a value type and then cannot be null!
Is there a solution ? I contacted my friend Christian and asked him if he knows about this issue. After a little IM conversation he suggested to implement a SoapExtension that assign default date times value to empty elements. Too much work for a basic problem. When we will have a DateTime nullable (reference type) ?