简体   繁体   English

如何在序列化类时更改属性,类层次结构

[英]How to change attributes, class hierarchy when a class is serialized

I have a base class in my SDK (Geometry) that serializes its members. 我的SDK(Geometry)中有一个基类,用于序列化其成员。 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. 现在,我需要适应工作流程的变化,这会导致基类成为父类的子级,这意味着XML层次结构也会发生变化。 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). 但是,使用以前的SDK代码(类层次结构)序列化的XML又如何呢? How do I de-serialize those XMLs into the new SDK? 如何将这些XML反序列化为新的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. 因此,让我们说在更改之后,会引入一个名为Calculator的类,它有一个Geometry集合。 But I want to rename Geometry as well as rename some of its elements/Attributes. 但是我想重命名Geometry以及重命名它的一些元素/属性。 And add/delete attributes too. 并添加/删除属性。

How do I go about it so that my previous XML's de-serialize successfully. 我如何去做,以便我以前的XML成功反序列化。

Have a look at Version Tolerant Serialization . 看看版本容忍序列化

This allows you to add tags (OptionalField, OnDeserializing, etc) to account for changes in the XML structure. 这允许您添加标记(OptionalField,OnDeserializing等)以考虑XML结构中的更改。

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 . 如果您可以选择更改,则某些序列化格式可以提供比XML更多的版本控制灵活性,例如协议缓冲区

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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