简体   繁体   English

具有向后兼容性的Xml阅读

[英]Xml Reading with backward Compatibility

The XML that i am currently working is directly formed using XML serializer (Serializing Class and its nested counter parts) 我当前正在使用的XML是使用XML序列化器(序列化类及其嵌套的计数器部件)直接形成的

Also if there is an addition of a new Property is directly handled by the serializer but the problem comes when there is a deletion of property (value Type) or removal of and entire class or addition of class 另外,如果添加了新的Property ,则由序列化程序直接处理,但问题是当deletion of property (value Type)removal of and entire class or addition of class

I wish to read the old as well as the new XML files.... I cant seem to figure out how.. 我希望阅读旧的和新的XML文件。。。我似乎无法弄清楚如何。

Process 处理

预期的XML升级过程

Some ways 从某种角度

But i don't think these are good for a maintainable code 但是我认为这些对于可维护的代码没有好处

1) Make the custom XML parser (this will be less flexible as every time the change is done the parser has to be updated and hence tested again). 1)制作自定义XML解析器(因为每次更改完成后解析器都必须更新并因此再次进行测试,因此灵活性较弱)。
2) Use multiple Models then migrate from old to new (Taking essential components) 2)使用多个模型,然后从旧模型迁移到新模型(采用基本组件)
3) Export Old file and import the new file (This will also require another XML file and may b related to point 2) 3)导出旧文件并导入新文件(这还将需要另一个XML文件,可能与第2点有关)
4) Any other means (Please suggest) 4)其他方式(请提出建议)

I am not well versed with XML and its versioning. 我不太熟悉XML及其版本控制。 Also is XML a good choice for this or Any other file type/DB that i can use in place of XML XML还是此文件或我可以用来代替XML的任何其他文件类型/ DB的不错选择

Any help in this regard would be helpful. 在这方面的任何帮助将是有帮助的。

I usually follow your solution "#2" where I namespace version my models (Myapp.Models.V1.MyModel), this way you can maintain backward compatibility with clients still using the older schema (or in your case, loading an older file). 我通常会遵循您的解决方案“#2”,在该解决方案中我对模型(Myapp.Models.V1.MyModel)进行命名空间版本化,这样您就可以保持与仍在使用旧模式的客户端的向后兼容性(或者在您的情况下,加载旧文件) 。

As suggested in the comments, you can use a simple attribute on the root node to determine the version, and use either xmlreader, or even a simple regex on the first line of the file to read the version number. 如注释中所建议,您可以在根节点上使用简单的属性来确定版本,并在文件的第一行使用xmlreader甚至简单的regex来读取版本号。

As far as your second question, about file type/db, depending on your needs, I would highly recommend looking at a document database like MongoDB or RavenDB, as implementation is straightforward/simple, and does not require the use of an ORM tool like entity framework to handle proper separation of concerns. 至于第二个关于文件类型/ db的问题,根据您的需要,我强烈建议您查看MongoDB或RavenDB之类的文档数据库,因为实现是直接/简单的,并且不需要使用ORM工具(例如实体框架来处理关注点的适当分离。 If you need something portable, in the cases such as desktop app "save file", SqlLite is a good file based databases, but you will likely want to use an ORM for mapping your model to your database. 如果您需要便携式的东西,例如桌面应用程序“保存文件”,则SqlLite是一个很好的基于文件的数据库,但是您可能希望使用ORM将模型映射到数据库。

Links: 链接:

In most ways, XmlSerializer already has pretty good version support built in. In most cases, if you add or remove elements it isn't a problem: extra (unexpected) data will be silently ignored - or put into the [XmlAnyElement] / [XmlAnyAttribute] member (if one) for round-trip. 在大多数情况下, XmlSerializer已经内置了相当不错的版本支持。在大多数情况下,如果添加或删除元素就没有问题:多余的(意外)数据将被静默忽略-或放入[XmlAnyElement] / [XmlAnyAttribute]成员(如果有)来回。 Any missing data just won't be initialized. 任何丢失的数据将不会被初始化。 The only noticeable problem is with sub-types, but adding and removing sub-types (or entire types) is going to be fairly fundamental to any serializer. 唯一值得注意的问题是子类型,但是添加和删除子类型(或整个类型)对于任何序列化器来说都是相当基本的。 One common option in the case of sub-types is: use a single model, but just don't remove any sub-types (adding sub-types is fine, assuming you don't need to be forwards compatible). 对于子类型,一个常见的选择是:使用单个模型,但不要删除任何子类型(添加子类型就可以了,假设您不需要前向兼容)。 However if this is not possible, the multiple models (model per revision) is not a bad approach. 但是,如果这不可能,则多个模型(每个修订版的模型)不是一个坏方法。

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

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