How to Deserialize Missing Fields To Nulls

Posted by Nathan on Stack Overflow See other posts from Stack Overflow or by Nathan
Published on 2010-04-26T22:42:42Z Indexed on 2010/04/26 22:43 UTC
Read the original article Hit count: 360

Filed under:
|
|
|
|

I am working on a .NET project where several versions of the same type of object have been created over the life of the project - most new versions add a field or two. However, now I am looking to mitigate problems for users with an old version.

My goal is that when a user tries to open an old version of the serialized object with the new program, the program will automatically assign default values to the fields that are missing from the depreciated version of the file.

For example, say I had a class with two boolean fields - RepeatSound and RepeatForever. The first version of the program doesn't know how to repeat forever, so it only saved RepeatSound:

<anyType xsi:type="Sound">
    <RepeatSound>true</RepeatSound>
</anyType>

but the new files look like this:

<anyType xsi:type="Sound">
    <RepeatSound>true</RepeatSound>
    <RepeatForever>true</RepeatForever>
</anyType>

My goal is that when I deserialize the first file in the new program that it will create the Sound object but simply default the value of RepeatForever to false. Right now trying to deserialize the first object would throw an exception.

Thank you in advance for any responses

© Stack Overflow or respective owner

Related posts about Xml

Related posts about serialize