XML(反)序列化和架构升级
我有一个复杂的 XML 可序列化类图,我可以很好地(反)序列化到硬盘。但是如何处理图模式结构的大规模更改?是否有某种机制来处理 XML 模式升级?一些类可以让我将旧数据迁移到新格式?
当然,我可以使用 XmlReader/XmlWriter,遍历每个节点和属性,写入几千个将数据转换为新格式的代码行,但也许有更好的方法?
I have a complex graph of XML-serializable classes that I'm able to (de)serialize to hard-disk just fine. But how do I handle massive changes to the graph schema structure? Is there some mechanism to handle XML schema upgrades? Some classes that would allow me to migrate old data to the new format?
Sure I could just use XmlReader/XmlWriter, go through every node and attribute and write several thousand lines of code to convert data to the new format, but maybe there is a better way?
I have found Object graph serialization in .NET and code version upgrades, but I don't think the linked articles apply when there are major changes in the model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 XSLT,而不是使用
XmlReader
/XmlWriter
编写数千行代码来转换文件。我们仍在谈论数百行代码,执行速度可能会较慢,但如果您擅长 XSLT,则可以更快地完成它。另一种方法是构建一个链接旧类和新类的 C# 程序(当然,您需要重命名旧类以避免命名冲突)。该程序将从磁盘加载
OldMyClass
,根据其属性值构造NewMyClass
,并将NewMyClass
序列化到磁盘。从本质上讲,这种方法将转换任务转移到了您可能更熟悉的 C# 领域。Instead of writing several thousand lines of code to convert files using
XmlReader
/XmlWriter
, you could use XSLT. We are still talking hundreds of lines of code, and perhaps slower execution speeds, but if you are good at XSLT you could get it done much faster.The other approach would be to build a C# program that links both the old class and the new class (of course you'd need to rename the old class to avoid naming collision). The program would load
OldMyClass
from disk, constructNewMyClass
from the values of its attributes, and serializeNewMyClass
to disk. Essentially, this approach moves the task of conversion into the C# territory, which may be a lot more familiar to you.在这种情况下,我将更改保留在对象中,并通过 XmlSerializer 重新创建 xml:http://support。 microsoft.com/kb/815813
有了这个,我加载并保存基于我的对象的新 xml 架构。
In this case, i keep my changes in my object and recreate my xml through the XmlSerializer: http://support.microsoft.com/kb/815813
With this i load and save new xml schema based in my object.