Compare 2 XML files in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • panipsilos
    New Member
    • Jan 2010
    • 2

    Compare 2 XML files in C#

    Hi , I need to compare 2 XML files in C# . I dont need to find the changes , but just to check if they are the same or not. Which is the best and fastest way to do it?

    thnx in advancee
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Depends on the how detailed you want to check.
    You could check the date/time of last modification.
    You could check their length (unless it is possible to be of identical length yet have different information inside).
    You could loop through both at the same time: Read line one of each and see if they are the same. Keep looping until you hit the end or a pair of lines that are different.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Here's an Microsoft KB article about making a byte-for-byte file comparer:

      It's pretty simple to understand, and will work for files other than XML as well.

      The only possible issue I see that you might have is that it is a byte-for-byte comparison. It will evaluate whitespace and other things that XML doesn't care about.

      For example,
      Code:
      <SomeNode />
      and
      Code:
      <SomeNode></SomeNode>
      Are exactly the same in XML, but the file comparer would consider the two different.

      Do you need to be that specific, or is what I linked good enough?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        XML is also pretty easy to traverse recursively. You could quite easily just step through the file's nodes and see if it matches the structure of the other file. That might also help you get by the issue insertAlias brought up, unless you want your program to catch that :)

        (XmlDocument, XmlNode, and XmlAttribute are the classes you might be interested in if you want to do a structure comparison)

        Comment

        • panipsilos
          New Member
          • Jan 2010
          • 2

          #5
          Guys thx a lot for your help.
          InsertAlias the comparer is great, I tried it and it works.

          Comment

          Working...