开发者

Compare 2 XML files using just C#

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜