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?
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.
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?
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