开发者

How to Deserialize Missing Fields To Nulls

I am working on a .NET project where several versions of the same type of object h开发者_运维技巧ave 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

EDIT: I am using the "XmlSerializer" class


I believe you're going to have to implement the [IXmlSerializable][1] interface to do the deserialization on your own.


See this page Especially the section about minOccurs=0 and the default value attribute

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜