如何将XML文件比较C#? [英] How to compare XML files in C#?

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

问题描述

我知道出现了很多这样的问题,但是我无法找到一个能够满足我的需求的答复。我必须写一个应用程序,将比较XML文件:将有2种类型的比较,首先为2档,列出所有的差异,第二个多个XML文件从均线列出所有的变化。

I know that there has been a lot of questions like this but I couldn't find a reply that would satisfy my needs. I have to write an application that will compare XML files: there will be 2 types of compare, first for 2 files, listing all the differences and second one for multiple XML files listing all the variations from averages.

我要寻找某种类,库或API的,这将帮助我完成这个任务。您能否提供一些解决方案?

I am looking for some kind of class, library or API that will help me finish this task. Can you suggest some solutions ?

可是,我不知道我是否应该使用DOM或XPath。有什么建议么 ?

And yet, I do not know if I should use DOM or Xpath. Any suggestions ?

编辑:

好了,所以我一直在试图完成该任务的xmldiff工具,但是这是很成问题要解决这个为多个XML文件 - 我不知道我怎么可以使用这个XmlDiffDiagram理清例如50 XML文件之间的差异

Ok so I have been trying to accomplish this task with XmlDiff tool but this is quite problematic to solve this for multiple Xml files - I have no idea how can I use this XmlDiffDiagram to sort out the differences among for instance 50 Xml files.

时它会与LINQ是更好?

Is it going to be better with LINQ ?

推荐答案

微软的 XML diff和patch API 应该很好地工作:

Microsoft's XML Diff and Patch API should work nicely:

public void GenerateDiffGram(string originalFile, string finalFile, XmlWriter diffGramWriter)
{
   XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
       XmlDiffOptions.IgnoreNamespaces | XmlDiffOptions.IgnorePrefixes);

   bool bIdentical = xmldiff.Compare(originalFile, finalFile, false, diffGramWriter);
   diffgramWriter.Close();
}

如果你需要,你还可以使用修补工具比较的文件并将其合并:

If you need to, you can also use the Patch tool to compare the files and merge them:

public void PatchUp(string originalFile, string diffGramFile, string outputFile)
{
    var doc = new XmlDocument();
    doc.Load(originalFile);

    using (var reader = XmlReader.Create(diffGramFile))
    {
        xmlpatch.Patch(sourceDoc, diffgramReader);

        using (var writer = XmlWriter.Create(outputFile))
        {
            doc.Save(writer);
            output.Close();
        }

        reader.Close();
    }
}

这篇关于如何将XML文件比较C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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