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.
我知道有很多这样的问题,但我找不到满足我需求的答复。我必须编写一个比较XML文件的应用程序:将有2种类型的比较,首先是2个文件,列出所有差异,第二个是多个XML文件,列出了平均值的所有变化。
I am looking for some kind of class, library or API that will help me finish this task. Can you suggest some solutions ?
我正在寻找某种类,库或API来帮助我完成这项任务。你能提出一些解决方案吗?
And yet, I do not know if I should use DOM or Xpath. Any suggestions ?
然而,我不知道我是否应该使用DOM或Xpath。有什么建议么 ?
EDIT:
编辑:
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.
好的,所以我一直在尝试使用XmlDiff工具完成这项任务,但是这对于多个Xml文件解决这个问题是非常有问题的 - 我不知道如何使用这个XmlDiffDiagram来梳理例如50个Xml文件之间的差异。
Is it going to be better with LINQ ?
LINQ会更好吗?
4 个解决方案
#1
18
Microsoft's XML Diff and Patch API should work nicely:
Microsoft的XML Diff和Patch API应该可以很好地工作:
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:
如果需要,您还可以使用Patch工具比较文件并合并它们:
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();
}
}
#2
5
If you want just to compare XML's and you don't need to get what is difference, you can use XNode.DeepEquals Method:
如果你只想比较XML,你不需要得到有什么区别,你可以使用XNode.DeepEquals方法:
var xmlTree1 = new XElement("Root",
new XAttribute("Att1", 1),
new XAttribute("Att2", 2),
new XElement("Child1", 1),
new XElement("Child2", "some content")
);
var xmlTree2 = new XElement("Root",
new XAttribute("Att1", 1),
new XAttribute("Att2", 2),
new XElement("Child1", 1),
new XElement("Child2", "some content")
);
Console.WriteLine(XNode.DeepEquals(xmlTree1, xmlTree2));
#3
3
Personally, I would go with LINQ to XML. You can find a good tutorial at: http://msdn.microsoft.com/en-us/library/bb387061.aspx
就个人而言,我会使用LINQ to XML。你可以在http://msdn.microsoft.com/en-us/library/bb387061.aspx上找到一个很好的教程。
#4
0
I wrote an xslt 1.0 sheet to compare one xml against another, and published it in my github here: https://github.com/sflynn1812/xslt-diff
我写了一个xslt 1.0表来比较一个xml和另一个xml,并在我的github上发布它:https://github.com/sflynn1812/xslt-diff
It is an unordered comparison so items that are not in b.xml but in the input xml will show even if the elements are elsewhere in the parent node.
这是一个无序比较,因此即使元素在父节点中的其他位置,也不会在b.xml中但在输入xml中的项目将显示。
#1
18
Microsoft's XML Diff and Patch API should work nicely:
Microsoft的XML Diff和Patch API应该可以很好地工作:
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:
如果需要,您还可以使用Patch工具比较文件并合并它们:
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();
}
}
#2
5
If you want just to compare XML's and you don't need to get what is difference, you can use XNode.DeepEquals Method:
如果你只想比较XML,你不需要得到有什么区别,你可以使用XNode.DeepEquals方法:
var xmlTree1 = new XElement("Root",
new XAttribute("Att1", 1),
new XAttribute("Att2", 2),
new XElement("Child1", 1),
new XElement("Child2", "some content")
);
var xmlTree2 = new XElement("Root",
new XAttribute("Att1", 1),
new XAttribute("Att2", 2),
new XElement("Child1", 1),
new XElement("Child2", "some content")
);
Console.WriteLine(XNode.DeepEquals(xmlTree1, xmlTree2));
#3
3
Personally, I would go with LINQ to XML. You can find a good tutorial at: http://msdn.microsoft.com/en-us/library/bb387061.aspx
就个人而言,我会使用LINQ to XML。你可以在http://msdn.microsoft.com/en-us/library/bb387061.aspx上找到一个很好的教程。
#4
0
I wrote an xslt 1.0 sheet to compare one xml against another, and published it in my github here: https://github.com/sflynn1812/xslt-diff
我写了一个xslt 1.0表来比较一个xml和另一个xml,并在我的github上发布它:https://github.com/sflynn1812/xslt-diff
It is an unordered comparison so items that are not in b.xml but in the input xml will show even if the elements are elsewhere in the parent node.
这是一个无序比较,因此即使元素在父节点中的其他位置,也不会在b.xml中但在输入xml中的项目将显示。