XML serialization


Last week i spend a few hours to analyse and fix a very strange problem. On the project where i am working on right now we have to serialize some objects to XML before we could send it to WebMQ. The objects that need to be serialized are created with the XSD command-line tool. We received an XSD from our customer and generated a class with this XSD.

XSD.exe /c /l:c# <XSD file>

These XSD’s are sometimes huge files so this will generate enormous classes. Before we send our XML reqeust to WebMQ we validate the XML against the XSD to be sure that WebMQ will accept and process our request. At this point i receive messages that the serialized XML isn’t valid, there are some elements missing. I started the debugger and checked if all the values of my object were set before it will be serialized. Everything looks fine, but the XML doens’t contain the values that it put in my object.

When i took a closer look to the XSD file i saw that every property that i have set in my object and isn’t in the XML file has the attribute minOccurs=”0″ defined. So i removed the minOccurs attribute from the XSD, generated a new class again with the XSD command-line tool and tried it again. This time every property was serialized to XML. I couldn’t  believe this was the solution for my problem. minOccurs=”0″ means the least amount of elements that should be in the XML file. So ‘0’ is acceptable but ‘1’ also. I think the problem is within the XSD command-line tool. The reason this took me so long to fix it, was because first i was looking in the wrong direction. I was searching for ingore and serialazable attributes never thought that the minOccurs in the XSD would be the problem. A colleague mentioned that when the XSD is changed which is my contract to communicate with WebMQ that this could introduce a new problem because my message could be invalid. fortunately this was not a problem i validated my XML against an XSD with the minOccurs attribute and my XML was valid.




One Response to XML serialization

  1. Pingback: XML serialization part II « Dennis van de Laar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: