XmlSerializer 等效于 IExtensibleDataObject [英] XmlSerializer equivalent of IExtensibleDataObject

查看:59
本文介绍了XmlSerializer 等效于 IExtensibleDataObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DataContracts,您可以从 IExtensibleDataObject 派生,以允许往返工作而不会丢失 XML 文件中的任何未知附加数据.

With DataContracts you can derive from IExtensibleDataObject to allow round-tripping to work without losing any unknown additional data from your XML file.

我不能使用 DataContract,因为我需要控制输出 XML 的格式.但我还需要能够在旧版本的应用程序中读取未来版本的 XML 文件,而不会丢失 XML 文件中的任何数据.

I can't use DataContract because I need to control the formatting of the output XML. But I also need to be able to read a future version of the XML file in the old version of the app, without losing any of the data from the XML file.

例如

XML v1:

<Person>
    <Name>Fred</Name>
</Person>

XML v2:

<Person>
    <Name>Fred</Name>
    <Age>42</Age>
</Person>

如果从我的应用程序的 v1 读取 XML v2 文件,反序列化并再次序列化它会将其转换为 XML v1 文件.即年龄"字段被删除.

If reading an XML v2 file from v1 of my app, deserializing and serializing it again turns it into an XML v1 file. i.e. the "Age" field is erased.

是否有任何类似于 IExtensibleDataObject 的东西可以与 XmlSerializer 一起使用以避免 Age 字段消失?

Is there anything similar to IExtensibleDataObject that I can use with XmlSerializer to avoid the Age field disappearing?

推荐答案

[XmlAnyAttribute][XmlAnyElement].

这篇关于XmlSerializer 等效于 IExtensibleDataObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆