468,505 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

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
Jan 25 '10 #1

✓ answered by Curtis Rutland

Here's an Microsoft KB article about making a byte-for-byte file comparer:
http://support.microsoft.com/kb/320348
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,
Expand|Select|Wrap|Line Numbers
  1. <SomeNode />
and
Expand|Select|Wrap|Line Numbers
  1. <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?

4 5161
tlhintoq
3,525 Expert 2GB
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.
Jan 25 '10 #2
Curtis Rutland
3,256 Expert 2GB
Here's an Microsoft KB article about making a byte-for-byte file comparer:
http://support.microsoft.com/kb/320348
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,
Expand|Select|Wrap|Line Numbers
  1. <SomeNode />
and
Expand|Select|Wrap|Line Numbers
  1. <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?
Jan 25 '10 #3
GaryTexmo
1,501 Expert 1GB
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)
Jan 25 '10 #4
Guys thx a lot for your help.
InsertAlias the comparer is great, I tried it and it works.
Jan 26 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Nicolas Fleury | last post: by
8 posts views Thread by Vincent | last post: by
2 posts views Thread by SP | last post: by
5 posts views Thread by drabee | last post: by
6 posts views Thread by yinglcs | last post: by
1 post views Thread by =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.