Here’s one for you. I have two Windows Server 2003 environments, and in one environment, a .NET object correctly serializes to XML, and in the next environment it does not.
Let’s set this up. First, I have an existing schema like below where my datetime/number types are both nillable, and have a minOccurs of 0. So, they could exist and be null, or not exist entirely.

Next, I generate a typed object for this schema using xsd.exe. The generated class contains my schema nodes, of course, but xsd.exe also inserts these boolean “[fieldname] + Specified” variables. Now these field accessors have the XmlIgnoreAttribute, so they don’t get included in the XML document, but rather can be used to check if a field exists. If the value is false, the the XML serializer doesn’t include the corresponding field in the output.

So far so good. I’ve built a really simple application that takes an XML string and loads it into my .NET object via the XmlSerializer Framework object. On my development machine, executing this step results in a MessageBox window that shows the object properties after the Xml deserialization occurred.

As you can see, all the values in my original XML document converted fine, and, the “specified” fields are both set to true because the corresponding fields have values. If I take this little application, and run it on our common development environment, I get the exact same result (I’ve also tested this on some co-worker’s machines). However, if I run this application in our TEST environment (same OS, same .NET framework version as previously tested environments), I get the following result:

What, what, what?? I still have values present for the integer (“Age”) and datetime (“BirthDate”) but the “specified” fields are now false. What’s the ramification? Turning this object back into XML in this TEST environment results in this …

Yowza. Now those fields don’t get serialized back into the XML document. Not good. As for solutions, the quickest one is to remove the auto-generated “specified” fields from the .NET object which results in everything serializing and deserializing just fine. However, I don’t like mucking with auto-generated code because you have to remember what changes you’ve made for all future releases.
Thoughts as to what could cause this? A .NET hotfix, something environmental? I’ve included my little test application here, so feel free to download and execute the quick test on your machine and post the results in the comments.
Technorati Tags: XML
Leave a comment