比较只用C#2 XML文件 [英] Compare 2 XML files using just C#

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

问题描述

我在寻找一种方式来比较XML文件(下面的示例),并创建包含XPath来在这两个文件的差异,所以我可以再查什么改变列表。

可以这样Linq中做了或我将需要使用MS打上补丁,我真的很想做这一切在C#中,没有任何额外的DLL。

或者我应该只是环槽一个XML文件中,并将其与其他的,但如果我这样做我可能会错过任何新的或删除节点!

文件A

 <信息>
  <检索>
    < LastNameInfo>
      &其中; LNameNum→1&其中; / LNameNum>
      &其中; NumPeople→1&其中; / NumPeople>
      <名称类型/>
      < LName的>测试与LT; / LName的>
    < / LastNameInfo>
    &其中;段>
      &其中; SegNum→1&其中; / SegNum>
      <注释>在测试< /注释和GT;
    < /段>
    &其中;段>
      &其中; SegNum→2&其中; / SegNum>
      < D​​T> 20110910< / DT>
      <注释> B测试< /注释和GT;
    < /段>
  < /检索>
< /信息>
 

文件B:

 <信息>
  <检索>
    < LastNameInfo>
      &其中; LNameNum→1&其中; / LNameNum>
      <名称类型/>
      < LName的>测试与LT; / LName的>
    < / LastNameInfo>
    &其中;段>
      &其中; SegNum→1&其中; / SegNum>
      <状态> HS< /状态>
      <注释>在测试< /注释和GT;
    < /段>
    &其中;段>
      &其中; SegNum→2&其中; / SegNum>
      < D​​T> 20110910< / DT>
      <注释>我可以使用LINQ< /注释和GT;
    < /段>
  < /检索>
< /信息>
 

输出的XPath列表

  /信息/检索/ LastNameInfo / NumPeople
/信息/检索/ LastNameInfo / LName的
/信息/检索/段[1] /状态
/信息/检索/段[2] /评论
 

解决方案

我假设你要比较的XML(的信息集)和文件的不只是词汇等价等价。 XML可以在它的词汇形式显著不同(选择报价的,insiginficant空白,订单的属性)。所有这些都可以在不影响信息集而变化。

我要规范化的文件 <一href="http://stackoverflow.com/questions/1026432/xml-canonicalization-algorithm-gives-two-difference-results-when-called-directly">XML规范化算法给出了两个不同的结果时,不是在调用时,XML数字签名的部分,直接叫什么名字? 和测试词汇的身份。如果它们不等同它可以是相当困难,以确定它们之间的区别,其中(微不足道的差异是比较容易的,但是,当元件顺序等是重要的,可能没有客观差异

我这样做对我自己的应用程序(在Java中,抱歉),这是不平凡的 - 我再次承担了文件之间相当接近等价。我还做了它这不​​得不在小量同意其中,词汇等价是不够的实数正常工作。

比较的xml在C#中一些的xmldiff工具

I'm looking for a way to compare to XML files (examples below) and create a list containing the xpath to the differences in the two files so I can then check what has changed.

can this be done in Linq or will I need to use MS Diff patch, I would really like to do it all in C# without any additional Dlls.

Or should I just loop trough one XML file and compare it to the other, but then if I do this I may miss any new or deleted nodes!

File A

<info>
  <Retrieve>
    <LastNameInfo>
      <LNameNum>1</LNameNum>
      <NumPeople>1</NumPeople>
      <NameType/>
      <LName>TEST</LName>
    </LastNameInfo>
    <Segment>
      <SegNum>1</SegNum>
      <Comment>A test</Comment>
    </Segment>
    <Segment>
      <SegNum>2</SegNum>
      <Dt>20110910</Dt>
      <Comment>B test</Comment>
    </Segment>
  </Retrieve>
</info>

File B:

    <info>
  <Retrieve>
    <LastNameInfo>
      <LNameNum>1</LNameNum>
      <NameType/>
      <LName>TESTING</LName>
    </LastNameInfo>
    <Segment>
      <SegNum>1</SegNum>
      <Status>HS</Status>
      <Comment>A test</Comment>
    </Segment>
    <Segment>
      <SegNum>2</SegNum>
      <Dt>20110910</Dt>
      <Comment>COULD I USE LINQ</Comment>
    </Segment>
  </Retrieve>
</info>

Output XPath list

/info/Retrieve/LastNameInfo/NumPeople
/info/Retrieve/LastNameInfo/LName
/info/Retrieve/Segment[1]/Status
/info/Retrieve/Segment[2]/Comment

解决方案

I assume you want to compare the equivalence of the XML (the Infoset) and not just the lexical equivalence of the files. XML can vary significantly in its lexical form (choice of quoting, insiginficant whitespace, order of attributes). All these can be varied without affecting the infoset.

I would canonicalize the files XML Canonicalization algorithm gives two difference results when called directly than when called as part of an xml digital signature? and test for lexical identity. If they are not equivalent it can be quite difficult to determine where they differ (trivial differences are relatively easy, but when element order, etc. is important there may be no objective difference.

I have done this for my own app (in Java, sorry) and it wasn't trivial - and again I assumed a fairly close equivalence between the files. I also made it work for real numbers which had to agree within epsilon where lexical equivalence isn't good enough.

see Xml Comparison in C# for some XMLDiff tools

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

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