C#学习教程:在C#中比较两个XML文件并使用XMLDiff生成第三个XML文件分享


在C#中比较两个XML文件并使用XMLDiff生成第三个XML文件

我正在尝试编写一个简单的算法来读取具有完全相同的节点和结构的两个XML文件,但不一定是子节点内的相同数据而不是相同的顺序。 我如何使用Microsoft的XML Diff .DLL创建一个简单的实现来创建第三个临时XML,即两个第一个之间的差异?

MSDN上的XML Diff:

XML差异和补丁工具

XML Diff和Patch GUI工具

示例要比较的两个不同XML文件的XML代码:

   Sidney Crosby PIT C 39 32 33 20 29      Sidney Crosby PIT C 42 35 34 22 30   

想要的结果(两者之间的差异)

    Sidney Crosby PIT C 3 3 1 2 1   

在这种情况下,我可能会使用XSLT将生成的XML“差异”文件转换为已排序的HTML文件,但我还没有。 我想要做的就是在第三个XML文件中显示每个节点的每个数值的差异,从“GP”子节点开始。

到目前为止C#代码:

 private void CompareXml(string file1, string file2) { XmlReader reader1 = XmlReader.Create(new StringReader(file1)); XmlReader reader2 = XmlReader.Create(new StringReader(file2)); string diffFile = StatsFile.XmlDiffFilename; StringBuilder differenceStringBuilder = new StringBuilder(); FileStream fs = new FileStream(diffFile, FileMode.Create); XmlWriter diffGramWriter = XmlWriter.Create(fs); XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreNamespaces | XmlDiffOptions.IgnorePrefixes); bool bIdentical = xmldiff.Compare(file1, file2, false, diffGramWriter); diffGramWriter.Close(); // cleaning up after we are done with the xml diff file File.Delete(diffFile); } 

这就是我到目前为止所做的,但结果是垃圾……注意,对于每个“播放器”节点,前三个孩子都没有被比较……我该如何实现呢?

有两个直接的解决方案:

解决方案1

您可以先对要删除不应比较的元素的两个文档应用简单转换。 然后,比较两个文档的结果 – 完全与您当前的代码。 这是转型:

           

将此转换应用于提供的XML文档时

   Sidney Crosby PIT C 39 32 33 20 29 10 1 3 0 154 20.8 21:54 22.6   

生成所需的结果文档

   39 32 33 20 29 10 1 3 0 154 20.8 21:54 22.6   

解决方案2。

这是一个完整的XSLT 1.0解决方案 (仅为方便起见,第二个XML文档嵌入在转换代码中):

       John Smith NY D 38 32 33 15 29 10 1 4 0 158 20.8 21:54 22.6                 -----------------------             

当此转换应用于与上述相同的第一个文档时,将生成正确的diffgrams

   39 20 3 154   -----------------------   38 15 4 158   

这是如何工作的

  1. 转换应用于第一个文档 ,将第二个文档作为参数传递。

  2. 这将生成一个XML文档,其唯一的叶元素节点是与第二个文档中相应的叶元素节点具有不同值的节点。

  3. 执行与上述1.相同的处理,但这次是在第二个文档上 ,将第一个文档作为参数传递。

  4. 这会产生第二个diffgram :一个XML文档,其唯一的叶元素节点是与第一个文档中相应的叶元素节点具有不同值**的节点

好的……我最终选择了一个纯C#解决方案来比较两个XML文件,而不使用XML Diff / Patch .dll,甚至不需要使用XSL转换。 我将在下一步中需要XSL转换,将Xml转换为HTML用于查看目的,但我已经计算出除了System.Xml和System.Xml.XPath之外什么都不使用算法。

这是我的算法:

 private void CompareXml(string file1, string file2) { // Load the documents XmlDocument docXml1 = new XmlDocument(); docXml1.Load(file1); XmlDocument docXml2 = new XmlDocument(); docXml2.Load(file2); // Get a list of all player nodes XmlNodeList nodes1 = docXml1.SelectNodes("/Stats/Player"); XmlNodeList nodes2 = docXml2.SelectNodes("/Stats/Player"); // Define a single node XmlNode node1; XmlNode node2; // Get the root Xml element XmlElement root1 = docXml1.DocumentElement; XmlElement root2 = docXml2.DocumentElement; // Get a list of all player names XmlNodeList nameList1 = root1.GetElementsByTagName("Name"); XmlNodeList nameList2 = root2.GetElementsByTagName("Name"); // Get a list of all teams XmlNodeList teamList1 = root1.GetElementsByTagName("Team"); XmlNodeList teamList2 = root2.GetElementsByTagName("Team"); // Create an XmlWriterSettings object with the correct options. XmlWriter writer = null; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); settings.OmitXmlDeclaration = false; // Create the XmlWriter object and write some content. writer = XmlWriter.Create(StatsFile.XmlDiffFilename, settings); writer.WriteStartElement("StatsDiff"); // The compare algorithm bool match = false; int j = 0; try { // the list has 500 players for (int i = 0; i < 500; i++) { while (j < 500 && match == false) { // There is a match if the player name and team are the same in both lists if (nameList1.Item(i).InnerText == nameList2.Item(j).InnerText) { if (teamList1.Item(i).InnerText == teamList2.Item(j).InnerText) { match = true; node1 = nodes1.Item(i); node2 = nodes2.Item(j); // Call to the calculator and Xml writer this.CalculateDifferential(node1, node2, writer); j = 0; } } else { j++; } } match = false; } // end Xml document writer.WriteEndElement(); writer.Flush(); } finally { if (writer != null) writer.Close(); } } 

XML结果:

    Sidney Crosby PIT C 0 0 0 0 0 0 0 0 0 0 0 0 0 0   Steven Stamkos TBL C 1 0 0 0 0 2 0 0 0 0 4 -0,6000004 -0,09999847 0,09999847  [...]  

我已经不遗余力地展示了CalculateDifferential()方法的实现,它相当神秘,但速度快,效率高。 通过这种方式,我可以在不使用任何其他参考但是严格的最小值的情况下获得所需的结果,而无需使用XSL ...

使用XSLT我使用树比较算法编写了一个Microsoft兼容的XSLT 1.0解决方案,以查找任何两个xml文件的差异。 我已将表单发布到我的github库中。 它输出它们之间存在差异的任何节点,但是如果找不到匹配则搜索兄弟节点。 工作表顶部的变量是您设置要比较的输入工作表的位置。

只有一些限制,它是有效的。

https://github.com/sflynn1812/xslt-diff

上述就是C#学习教程:在C#中比较两个XML文件并使用XMLDiff生成第三个XML文件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---猴子技术宅(www.ssfiction.com)

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1000538.html

(0)
上一篇 2021年12月27日 下午9:50
下一篇 2021年12月27日 下午9:52

精彩推荐

发表评论

您的电子邮箱地址不会被公开。

=========================={"status":"error","error_msg":"Can not download remote image file by use curl! http_code:301<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:403<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:0<\/span>"}{"status":"error","error_msg":"cURL error 6: Could not resolve: www.irie.cn (Domain name not found)<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:403<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}{"status":"error","error_msg":"Can not download remote image file by use curl! http_code:404<\/span>"}{"status":"error","error_msg":"Can not download remote image file!<\/span>"}