我应该如何管理基于XML文档的不同不相容formts [英] How should I manage different incompatible formts of Xml based documents

查看:134
本文介绍了我应该如何管理基于XML文档的不同不相容formts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我节省了文件(认为word文档)的基于XML的格式的应用程序 - 从XSD文件生成目前C#类用于读/写文件的格式,一切都很好,直到最近,当我不得不做出改变该文件的格式。我关心的是向后兼容我的应用程序的需要的未来版本以能够读取所有以前的版本保存的文档,最好我也希望老版本的我的应用程序才能够正常处理阅读文件保存通过我的应用程序的未来版本。

I have an application which saves documents (think word documents) in an Xml based format - Currently C# classes generated from xsd files are used for reading / writing the document format and all was well until recently when I had to make a change the format of the document. My concern is with backwards compatability as future versions of my application need to be able to read documents saved by all previous versions and ideally I also want older versions of my app to be able to gracefully handle reading documents saved by future versions of my app.

例如,假设我改变我的文档的架构中添加一个可选的额外元素的地方,然后旧版本我的应用程序将简单地忽略额外elemnt会有任何问题:

For example, supposing I change the schema of my document to add an (optional) extra element somewhere, then older versions of my application will simply ignore the extra elemnt and there will be no problems:

<doc>
	<!-- Existing document -->
	<myElement>Hello World!</myElement>
</doc>



然而,如果一个重大更改是由(一个属性被改变为例如一个元件,或者一个集合要素),那么过去我的应用程序版本应该要么忽略这个元素,如果它是可选的,或者告知他们正试图读取保存,否则我的应用程序的新版本的文档的用户。也这是目前令我头疼的是我的应用程序的所有未来版本需要需要读取两个不同的文件完全独立的代码。

However if a breaking change is made (an attribute is changed into an element for example, or a collection of elements), then past versions of my app should either ignore this element if it is optional, or inform the user that they are attempting to read a document saved with a newer version of my app otherwise. Also this is currently causing me headaches as all future versions of my app need entirely separate code is needed for reading the two different documents.

这种变化的一个例子是以下XML:

An example of such a change would be the following xml:

<doc>
	<!-- Existing document -->
	<someElement contents="12" />
</doc>

更改为:

<doc>
	<!-- Existing document -->
	<someElement>
	    <contents>12</contents>
		<contents>13</contents>
	</someElement>
</doc>

为了防止支持头痛,今后我要拿出处理变化的体面战略我可能会在将来,让我释放现在要去我的应用程序的版本,才能够应对这些变化在未来的:

In order to prevent support headaches in the future I wanted to come up with a decent strategy for handling changes I might make in the future, so that versions of my app that I release now are going to be able to cope with these changes in the future:


  • 如果文档的版本号存储在文档中,如果有的话是什么版本的策略应使用?如果文件版本匹配中的.exe程序集的版本,还是应该更复杂的策略中使用的,(例如主要修订变化表明重大更改,wheras次版本增量显示非破坏性变更 - 例如额外的可选元素)

  • 我应该使用什么方法来读取文件本身,如何避免复制代码大量不同版本的文件?

    • 虽然XPath的显然是最灵活的,这是一个大量的工作不是简单地生成与XSD类来实现。

    • 在其他相反,如果DOM解析被使用,那么就需要在每一个重大更改源代码控制文件的XSD的新副本,造成的问题,如果修复以往任何时候都需要应用到旧的模式(旧版本的应用程序仍然支持)。

    另外,我工作了这个非常松耦合的假设,所有的变化我提出可以分为这两类beaking变化和不间断的变化,但我不完全相信,这是一个安全的假设作出。

    Also, I've worked all of this very loosly on the assumption that all changes I make can be split into these two categories of "beaking changes" and "nonbreaking changes", but I'm not entirely convinced that this is a safe assumption to make.

    注意,我使用文件非常松散 - !内容不类似的文件在所有

    Note that I use the term "document" very loosely - the contents dont resemble a document at all!

    感谢什么建议可以提供给我

    Thanks for any advice you can offer me.

    推荐答案

    您一定需要XML文件中的一个版本号,我会建议它不绑到应用程序的版本,因为它是真正的独立实体。您可以通过您的应用程序的两个或三个版本,而没有改变的XML格式,或者你可能结业单一的发行开发过程中多次更改格式。

    You definitely need a version number in the XML file, and I would suggest not tying it to the version of the application because it's really a separate entity. You may through two or three versions of your app without ever changing the XML format or you may wind up changing the format multiple times during development of a single release.

    如果你想更旧版本的应用程序,以便能够读取XML文件的较新版本,那么你可以永远,永远删除元素或更改其名称。您可以随时添加元素和旧的代码会高兴地忽略它们(XML的优越的功能之一),但如果删除它们,然后旧的代码将无法正常工作。

    If you want older versions of the application to be able to read newer versions of the XML file then you can never, ever remove elements or change their names. You can always add elements and the older code will happily ignore them (one of the nice features of XML) but if you remove them then the old code won't be able to function.

    像伊斯梅尔说,XSLT是将XML格式从一个版本转换为另一种,这样你就不会用一大堆在源代码分析例程的拉闸的好方法。

    Like Ishmael said, XSLT is a good way to convert the XML format from one version to another so that you don't wind up with a whole pile of parsing routines in your source code.

    这篇关于我应该如何管理基于XML文档的不同不相容formts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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