开发者

saving and restoring xml differences

I am using asp.net and the .net framework 2.0. I may be able to upgrade the servers to 3.5 if the solution is compelling enough. Here is the problem.

I have two pieces of xml. I'll refer to piece number 1 as the template and piece number 2 as the actual.

Here's a basic example:

template:

<questions>
 <question1 msg="1234">
   <answer></answer>
 &开发者_运维技巧lt;/question1>
 <question2 msg="1235">
   <answer></answer>
 </question2>
 <question3 msg="">
   <answer></answer>
 </question3>
</questions>

actual:

<questions>
 <question1 msg="1234">
   <answer>foo</answer>
 </question1>
 <question2 msg="1235">
   <answer>bar</answer>
 </question2>
 <question3 msg="dynamic">
   <answer>blob</answer>
 </question3>
</questions>

The template is generic and common to many users, then there is the actual which is specific to individual users.

I would like to extract the delta between the actual and the template in such a way that it can be saved independently and then subsequently re-applied to the template to arrive at a complete representation of the actual xml.

I've done some looking and found an "XML Diff and Patch" tool for the .net 1.0 that looks like it does pretty much exactly what I need but then I found some other references to it that seem to indicate it has dropped off the radar. http://msdn.microsoft.com/en-us/library/aa302294.aspx

I've also found a number of examples which rely on the specific xml structure to manually extract the differences between the entities represented by the xml. I am generally uncomfortable with this solution and would really prefer a more generic one that is resilient to changes made to the xml.

Ideally, I'd love to find the xmldiff/patch functionality built into .net2.0/3.5 somewhere. If not, then something which solves the above problem in a generic enough way that it doesn't break when the xml changes.

Thanks


I think you may be over-engineering this. Although a diff/patch tool may meet your needs, it seems to me that something less general would also meet your needs.

In your example, the <answer/> tag is always present in the template, and is always empty with no attributes. All of the child elements of the <questions/> tag have names beginning with "question", and they all have a msg attribute, whose value is either an integer or blank. If it's an integer, then its value matches the corresponding value from the "actual" file, but if blank it can match "dynamic".

Given these constraints, the set of possible differences is a lot simpler to describe: it's just the content of the <answer/> element under each question. This is much easier to reason about than a general purpose diff utility would be.


I obtained the xmlDiffPatch package from Microsoft at the following url: http://download.microsoft.com/download/xml/Patch/1.0/WXP/EN-US/xmldiffpatch.exe

It works perfectly in my .net 2.0 solution. Using this technique I have been able to reduce the amount of data stored to between 2% and 25% of the amount of data that we previously needed to store.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜