Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deserializing nullable types 1

Status
Not open for further replies.

LV

Programmer
Nov 1, 2000
1,184
US
I know it's a known issue, and the solution is to change ptoperty type to string. I would hate to do that and hopefully someone knows a workaround. Trying to derserialize an XML to a class that has a nullable DateTime property. It's all good when the xml doesn't contain a corresponding element, the problem comes when the element is there but empty - getting a deserialization error. The sample is below, thanks in advance!

Code:
XML:
<address>
 <streetAddress>1914 DEHESA</streetAddress>
 <city>EL CAJON</city>
 <state>CA</state>
 <postalCode>92019</postalCode>
 [b]<dateReported>1998-04-20</dateReported>[/b]
</address>
[code]

[code]
C#:
....
        private DateTime? dateReported;
        [XmlElement("dateReported", IsNullable=true)]
        public DateTime? DateReported
        {
            get { return dateReported; }
            set { dateReported = value; }
        }
... 
}
 
Hey Lev!

Did you figure this out? I think for "null" types, XmlSerializer expects something like this:

Code:
<!-- assumes xsi namespace is declared -->
<date xsi:nil="true" />

Instead of an empty element. Worst case scenario you could implement IXmlSerializable or run an XSL transform on the XML before deserializing it to get an empty value into the above format, but I personally wouldn't know a cleaner way to do it.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Hey Colin,

Same place to meet, huh? Yes, I ended up with runing an XSL transform on the XML before deserializing in order not render elements that correspond to the nullable properties in the class - that seems to be the only way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top