简体   繁体   中英

How to change attributes, class hierarchy when a class is serialized

I have a base class in my SDK (Geometry) that serializes its members. Some members are just simple attributes & Elements like strings, and others are arrays of user defined classes. Now, I need to accommodate a change in my workflows which causes the base class to be child of a parent class which means the XML hierarchy will change as well. And, also I want to rename some of the attributes/elements.

But, what about the XMLs that were serialized with previous SDK code (class hierarchy). How do I de-serialize those XMLs into the new SDK?

<Geometry Type"Test" IsAssessed="False">
    <Name>Sample Geometry</Name>
    <LengthA>69</LengthA>
    <LengthB>87</LengthB>
    <LengthC>50</LengthC>
        <Points>
            <Point X="1537308.5" Y="16030594.72" Z="1000">
                <IsTracked>false</IsTracked>
            </Point>
            <Point X="1537308.5" Y="16030594.72" Z="900">
                <IsTracked>false</IsTracked>
            </Point>
            <Point X="1536601.21" Y="16028954.3" Z="-5670.6">
                <IsTracked>false</IsTracked>
            </Point>
        </Points>
</Geometry>

So, Lets say after change is Made, a class is introduced whose name is Calculator and it has a collection of Geometry. But I want to rename Geometry as well as rename some of its elements/Attributes. And add/delete attributes too.

How do I go about it so that my previous XML's de-serialize successfully.

Have a look at Version Tolerant Serialization .

This allows you to add tags (OptionalField, OnDeserializing, etc) to account for changes in the XML structure.

Alternatively, if it's a once off you could de-serialize into the old version and then implement conversion code to update to the new version. This could get very difficult to maintain if it's something that's regularly happening though.

If you have the option to change, some serialization formats offer more flexibility around versioning than XML, eg, Protocol Buffers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM