编程XML比较/合并在C# [英] Programmatic XML Diff / Merge in C#

查看:129
本文介绍了编程XML比较/合并在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前时刻,我管理一块具有多个XML配置文件的软件。当软件的新版本发布时,有时基本配置文件改变,我们目前有软件调用 KDiff 上启动。如果检测到一个变化时,它提示用户选择的变化。

At this moment, I am managing a piece of software that has multiple XML configuration files. When a new version of software is released, sometimes the base config files change, we currently have the software call KDiff on startup. If it detects a change, it prompts the user to choose the changes.

这种方法的问题在于,KDiff是行比较程序和不知道的方式XML(像节点等)

The problem with this approach is that KDiff is a line comparing program and not aware of the ways of XML (like Nodes, etc.)

在理想情况下,我想以编程方式在C#库工作(因为我们是一个MS店),可以DIFF两个XML文件:a。源XML和当前工作XML

Ideally, I would like to programmatically work with a library in C# (since we're a MS shop) that can Diff two XML files: a Source XML and a Current Working XML.

然后合并两者结合起来使用一些简单的规则:

And then Merge the two together using a few simple rules:


  1. 如果当前的工作XML有一个节点是源XML没有,删除它。

  2. 如果源XML有一个节点,目前的工作XML不,添加它。

  3. 如果两者有相同的节点和值不同,有利于源XML的价值,除非源XML的值设置为UseExistingValue。

例如,这里的源XML:

For example, here's the "Source" XML:

<Configuration>
  <Items>
     <Item Id="1" Position="true">
       <Location X="UseExistingValue" Y="UseExistingValue" Z="UseExistingValue" />

       <Something/>
       <SomethingElse/>
     </Item>
   </Items>
 </Configuration>



而这里的当前工作XML:

And here's the "Current Working" XML:

<Configuration>
  <Items>
    <Item Id="1" Position="false">
      <Location X="123" Y="234" Z="345" />
      <Another/>
      <Something/>

    </Item>
  </Items>
</Configuration>

和合并后的版本将是这样的:

And the merged version would look like:

<Configuration>
  <Items>
    <Item Id="1" Position="true">
      <Location X="123" Y="234" Z="345" />

      <Something/>
      <SomethingElse/>
    </Item>
  </Items>
</Configuration>



我看了的 MS XML diff和patch工具,它肯定合并文件一起,但不允许,我要定义的程序规则。

I've looked at the MS XML Diff and Patch Tool and it definitely merges the files together, but doesn't allow for the programmatic rules that I want to define.

XMLUnit对Java开发者似乎看好,但它的.NET版本似乎不发达,这是不幸的

XMLUnit for Java devs seems promising, but the .NET version of it seems underdeveloped, which is unfortunate.

任何人有任何脚本化的XML的比较/合并工具和/或.NET库(付费或免费)?

Anyone have any suggestions for either scriptable XML Diff/Merge tools and/or .NET libraries (paid or free)?

感谢。

推荐答案

乱搞的几天后,我发现了一个解决方案,我想对我的作品。也许这可能适用于其他人也是如此。

After a couple days of messing around, I found a solution that I think works for me. Maybe it could work for other people as well.

的MS XML diff和patch工具是一个可行的选择。当你第一次DIFF对文件第二个文件时,它会创建一个XML的DiffGram上市了两个XML文件之间检测到有什么变化。

The MS XML Diff and Patch tool was a viable option. When you Diff first file against the second file it creates an XML "DiffGram" listing what changes it detected between the two XML files.

要充分利用我上面列出的所有3规则照顾,我Diff'd在一个方向的两个文件,然后打开使用的DiffGram文件Linq到XML和删除了所有添加和删除行。

To take care of all 3 rules that I listed above, I Diff'd the two files in one direction, then opened the DiffGram file using Linq-to-XML and Removed all the "Add" and "Remove" lines.

XNamespace xd = "http://schemas.microsoft.com/xmltools/2002/xmldiff";
var doc = XDocument.Load(_diffGramFile);
doc.Root.DescendantsAndSelf(xd + "add").Remove();
doc.Root.DescendantsAndSelf(xd + "remove").Remove();



然后我修补(合并)对第一个文件,编辑过的DiffGram,并创建了部分合并临时文件。这个负责规则1和2的

Then I patched up (merged) this edited diffgram against the first file and created a partially merged temporary file. This takes care of Rules 1 and 2.

接着,我Diff'd部分合并文件对所使用的第一个文件。然后打开了新的DiffGram,并删除了所有更改提及UseExistingValue

Next, I Diff'd the partially merged file against the first file used. Then opened the new DiffGram and removed all Change references to "UseExistingValue".

var newdoc = XDocument.Load(_diffGramFile);
newdoc.Root.DescendantsAndSelf(xd + "change")
      .Where(x => x.Value == "UseExistingValue").Remove();

和合并编辑过的DiffGram对部分合并的文件这需要规则的照顾3.保存该出XML然后产生根据上面定义的规则合并最终的XML。

And merged this edited DiffGram against the partially merged file which takes care of Rule 3. Saving this out to XML then produces the final XML merged according to the rules defined above.

希望这可以帮助其他人。

Hopefully this can help out other people.

提示:在安装XmlDiffPatch图书馆,XmlDiffPatch DLL后能在C发现:\Windows\assembly\GAC\XmlDiffPatch\1.0.8.28__b03f5f7f11d50a3a\XmlDiffPatch.dll

HINT: After installing the XmlDiffPatch library, the XmlDiffPatch DLL can be found in C:\Windows\assembly\GAC\XmlDiffPatch\1.0.8.28__b03f5f7f11d50a3a\XmlDiffPatch.dll

这篇关于编程XML比较/合并在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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