简体   繁体   English

处理XML变更的好方法

[英]Good Way To Handle XML Change

Our system stores XML strings in a database. 我们的系统将XML字符串存储在数据库中。 I've recently had to change the Properties on a Class, and now when an XML string gets deserialized it will throw an exception. 我最近不得不更改类的属性,现在当XML字符串反序列化时,它将引发异常。 What is the best way to handle this change? 处理此更改的最佳方法是什么? Look for the Node in the application code using XPATH or LINQ, or change the xml string in sql database (ie do a mass update)?. 使用XPATH或LINQ在应用程序代码中查找Node,还是更改sql数据库中的xml字符串(即进行批量更新)?

You might want to look at writing a custom XML deserializer (ie implementing IXmlSerializable, see here ) to handle changes in your XML. 您可能想要看一下编写自定义XML反序列化器(即实现IXmlSerializable,请参阅此处 )来处理XML中的更改。 If you've invested a lot of time into crafting your XML serialization attributes, you may want to look at another approach. 如果您花费大量时间来设计XML序列化属性,则可能需要考虑另一种方法。

Consider batch-upgrading your XML, or deprecating (instead of removing) properties inside of your classes and mapping older behavior to newer behavior. 考虑批量升级XML,或弃用(而不是删除)类内部的属性,并将较旧的行为映射为较新的行为。

Longer term, you will want to come up with a strategy for dealing with this in the future, since you will most likely be continue to make changes to your schema/object definitions as you add/change the functionality of your system. 从长远来看,您将来会想出一个解决该问题的策略,因为在添加/更改系统功能时,很可能会继续更改架构/对象定义。

if you serialize the objects to the database you could try the approach I outlined here to load the old versions into a new version then when you save the new version will be saved. 如果将对象序列化到数据库,则可以尝试使用此处概述的方法将旧版本加载到新版本中,然后在保存新版本时保存。 Not sure if having different versions of your class will be appropriate though... 不确定使用不同版本的课程是否合适...

Basically you create a factory to produce your objects from the xml. 基本上,您创建了一个工厂来从xml生成对象。 everytime you change your object you create a new factory and a new object class, which is given a version of the old class in its constructor and it creates itself from the old class. 每次更改对象时,都会创建一个新的工厂和一个新的对象类,该类在其构造函数中得到旧类的版本,并从旧类中创建自己。 The new factory tries to create a new object from the xml, if it can, happy days, if it can't then it creates a new object and tells the next oldest factory to create a next oldest object from the xml. 如果可以,新工厂将尝试从xml创建一个新对象,如果不能,那么它将创建一个新对象,并告诉下一个最旧的工厂从xml创建下一个最老的对象。 These factories can then be chained together so that you can always load a newest version of the objects from whatever data is in the db. 然后可以将这些工厂链接在一起,以便您始终可以从数据库中的任何数据加载对象的最新版本。

This assumes that its possible to always create a valid v2 object from a v1 object. 假设可以始终从v1对象创建有效的v2对象。

It's a good practice to store a version along your XML strings. 沿XML字符串存储版本是一个好习惯。 Either at the database level or at the class level so that your code knows which version of the class it has to deserialize. 在数据库级别或在类级别,以便您的代码知道必须反序列化该类的版本。

You might also look at XSLT. 您可能还会看看XSLT。 It allows you to transform one version of XML into another. 它允许您将一个XML版本转换为另一个XML版本。 In that case the logic to go from one version to another is not handle by code but by the XSLT. 在那种情况下,从一个版本过渡到另一个版本的逻辑不是由代码而是由XSLT处理。 You can even store the XSLT into the database which makes it reusable by other programs. 您甚至可以将XSLT存储到数据库中,使其可以被其他程序重用。

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

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