Compare 2 XML files using just C#

倾然丶 夕夏残阳落幕 提交于 2019-12-23 04:04:45

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/4660971/compare-2-xml-files-using-just-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!