Groups | Blog | Home
all groups > dotnet xml > july 2004 >

dotnet xml : Deserializing NULLs into Integer variables


Mark McConnell
7/28/2004 6:08:23 PM
Regarding deserializing XML into a custom object...

I've been able to deserialize an XML doc into my custom object and
everything works great. The problem I am encountering is when one of the
XML elements contains a NULL and it maps to a variable of type Integer in my
custom object, the deserializer doesn't like this.

For example, my custom object is declared like:

Public Class Member
Public Height As Integer
End Class


So, if I use the following XML element:

<Height></Height>
or
<Height xsi:null='1' />

the deserializer throws an exception (SystemInvalidOperationException:
"There is an error in the XML document.") when trying to assign an empty
string (meant to represent NULL) to the Integer value. This makes sense and
is expected behavior.

If I completely remove the <Height> element from the XML in order to
represent a NULL value, the deserializer works fine, but the custom object's
Integer variable (Height) is set to 0 (which isn't NULL). Shouldn't the
variable be set to Nothing? Should I not use the deserializer and manually
check each XML element value before assigning them to my custom object's
variables? What is the correct methodology for mapping SQL data that may
contain NULLs into a custom object that uses Integers and String variable
types? Can the deserializer use an XSD schema so that it knows that an
empty XML element indicates NULL for an Integer type?

I'd appreciate any help you can offer.

Thanks in advance!
Mark

Dino Chiesa [Microsoft]
7/28/2004 7:15:04 PM
Mark
This is an example of the "impedance mismatch" between W3C XML Schema and
the .NET type system. W3C XML Schema says that Integers can be nillable.
..NET says integers are value types, and may never have null values.

But there is an easy way to handle it, using the xxxSpecified convention.

Here's an illustration. (using DateTime, not int. But the same principle
applies. And my apologies: it is in C#. )
http://www.winisp.net/cheeso/srcview.aspx?dir=xml-serialization&file=DateOptional.cs

If the Schema you are working from has a nillable="true" attribute on the
field or fields in question, then the xsd.exe tool (part of .NET SDK) will
properly generate the xxxSpecified fields tagged with [XmlIgnore] to handle
this situation. If your class does not have this, then either :
- your schema is broken
- the doc you have is not conformant to the schema (your doc is broken)
- you are not working from a schema at all.

Ideally you would be able to fix the root cause.

-Dino

ps:
This is the third time in 3 days I have answered this question on this
newsgroup.
http://groups.google.com/groups?q=dateoptional.cs&num=30&hl=en&lr=&ie=UTF-8&sa=N&tab=wg
I guess we need to document it better. or publish an up-to-date FAQ list.
Or something. Suggestions?

--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ OmitThis . m i c r o s o f t . c o m


[quoted text, click to view]

Mark McConnell
7/29/2004 9:23:29 AM
Dino,

Thank you and I will give it a try!

I read throught he newsgroup looking for similar questions so that I
wouldn't be redundant, but I didn't find any that had matched my exact
problem. Sorry for the redundancy and thank you for your help!!

Mark


[quoted text, click to view]

Dino Chiesa [Microsoft]
7/29/2004 9:32:48 AM
no worries on the redundancy, mate.
but I am looking for suggestions on how to make it easier for people like
you to find the answers that have already been provided.

(Also - I just finished answering another form of this same question on
another newsgroup. Heh heh. ).
-D


[quoted text, click to view]

AddThis Social Bookmark Button