简体   繁体   English

C#中的Xml比较

[英]Xml Comparison in C#

I'm trying to compare two Xml files using C# code. 我正在尝试使用C#代码比较两个Xml文件。 I want to ignore Xml syntax differences (ie prefix names). 我想忽略Xml语法差异(即前缀名称)。 For that I am using Microsoft's XML Diff and Patch C# API. 为此我使用的是Microsoft的XML Diff和Patch C#API。 It works for some Xml's but I couldn't find a way to configure it to work with the following two Xml's: 它适用于某些Xml,但我找不到配置它来使用以下两个Xml的方法:

XML A: XML A:

<root xmlns:ns="http://myNs">
  <ns:child>1</ns:child>
</root>

XML B: XML B:

<root>
  <child xmlns="http://myNs">1</child>
</root>

My questions are: 我的问题是:

  1. Am I right that these two xml's are semantically equal (or isomorphic)? 我是对的,这两个xml在语义上是相等的(或同构的)吗?
  2. Can Microsoft's XML Diff and Patch API be configured to support it? 可以配置Microsoft的XML Diff和Patch API来支持它吗?
  3. Are there any other C# utilities to to this? 还有其他C#实用程序吗?

The documents are isomorphic as can be shown by the program below. 这些文件是同构的,如下面的程序所示。 I think if you use XmlDiffOptions.IgnoreNamespaces and XmlDiffOptions.IgnorePrefixes to configure Microsoft.XmlDiffPatch.XmlDiff , you get the result you want. 我想如果你使用XmlDiffOptions.IgnoreNamespacesXmlDiffOptions.IgnorePrefixes配置Microsoft.XmlDiffPatch.XmlDiff ,你会得到你想要的结果。

using System.Linq;
using System.Xml.Linq;
namespace SO_794331
{
    class Program
    {
        static void Main(string[] args)
        {
            var docA = XDocument.Parse(
                @"<root xmlns:ns=""http://myNs""><ns:child>1</ns:child></root>");
            var docB = XDocument.Parse(
                @"<root><child xmlns=""http://myNs"">1</child></root>");

            var rootNameA = docA.Root.Name;
            var rootNameB = docB.Root.Name;
            var equalRootNames = rootNameB.Equals(rootNameA);

            var descendantsA = docA.Root.Descendants();
            var descendantsB = docB.Root.Descendants();
            for (int i = 0; i < descendantsA.Count(); i++)
            {
                var descendantA = descendantsA.ElementAt(i);
                var descendantB = descendantsB.ElementAt(i);
                var equalChildNames = descendantA.Name.Equals(descendantB.Name);

                var valueA = descendantA.Value;
                var valueB = descendantB.Value;
                var equalValues = valueA.Equals(valueB);
            }
        }
    }
}

I know that you're focus isn't on unit tests, but XMLUnit can compare two XML files and I think it's able to solve your example. 我知道你关注的不是单元测试,而是XMLUnit可以比较两个XML文件,我认为它能够解决你的例子。 Maybe you could look at the code ahd figure out your solution. 也许你可以看看代码啊你弄清楚你的解决方案。

I've got an answer by Martin Honnen in XML and the .NET Framework MSDN Forum. 我得到了Martin Honnen在XML和.NET Framework MSDN论坛上的答案 In short he suggests to use XQuery 1.0's deep-equal function and supplies some C# implementations. 简而言之,他建议使用XQuery 1.0的深度相等功能并提供一些C#实现。 Seems to work. 似乎工作。

It might be an idea to load XmlDocument instances from each xml file, and compare the XML DOM instead? 从每个xml文件加载XmlDocument实例可能是一个想法,并比较XML DOM? Providing the correct validation is done on each, that should give you a common ground for a comparison, and should allow standard difference reporting. 对每个进行正确的验证,这应该为您提供进行比较的共同点,并且应该允许标准差异报告。 Possibly even the ability to update one from the other with the delta. 可能甚至能够用delta更新另一个。

Those documents aren't semantically equivalent. 这些文件在语义上不相同。 The top-level element of the first is in the http://myNS namespace, while the top-level element of the second is in the default namespace. 第一个的顶级元素位于http://myNS命名空间中,而第二个元素的顶级元素位于默认命名空间中。

The child elements of the two documents are equivalent. 这两个文件的元素是等价的。 But the documents themselves aren't. 但文件本身并非如此。

Edit: 编辑:

There's a world of difference between xmls:ns='http://myNS' and xmlns='http://myNS' , which I appear to have overlooked. xmls:ns='http://myNS'xmlns='http://myNS'之间存在着xmlns='http://myNS' ,我似乎忽略了这一点。 Anyway, those documents are semantically equivalent and I'm just mistaken. 无论如何,那些文件在语义上是等价的,我只是错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM