473,394 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 5454
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

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

Similar topics

4
by: Nicolas Fleury | last post by:
Hi everyone, Is there a way to compare recursively two objects (compare their members recursively)? I'm only interested in equality or non-equality (no need for lower-than...). Thx and...
1
by: Frank | last post by:
Hi, is there a way to use XSL to compare two XML files to verify if a "record" in an XML file has changed of parent in another XML file ? I am trying to implement a template in an XSL stylesheet...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
2
by: SP | last post by:
Hi All, I need to compare two files line by line and copy the differing lines to a new files. I.e. Compare file1 and file2 line by line. Copy only differing lines to file3. I tried a couple...
5
by: drabee | last post by:
Please help 2 things: 1-I need c# code to compare 2 audio files .or any other .net code 2-code to receive bluetooth file from mobile and save it using .net code
0
by: bobbymusic | last post by:
In my form I have two rich text boxes.In both of them I put two different text files(.txt or .rtf).I looking for some code to compare them, and to have the result as marked differences into the...
6
by: yinglcs | last post by:
Hi, i have 2 files which are different (1 line difference): $ diff groupresult20070226190027.xml groupresult20070226190027-2.xml 5c5 < x:22 y:516 w:740 h:120 area: --- But when I use the...
8
by: Perl Beginner | last post by:
I am new to Perl and new to this site. I have the same question that I keep seeing, but not finding an answer…why doesn’t the compare function work? I’ve been going at this for a while. My code is...
1
by: =?Utf-8?B?RGFuaWVsIERpIFZpdGE=?= | last post by:
II want to compare how many seconds there are between files. If the files are within a 1 - 10 second range I want to copy them to their own folders. What I have so far is a couple methods that take...
12
by: blackirish | last post by:
Hi all, I am trying to merge 2 XML files that first of all i need to compare nodes of both files according to 2 attributes in the nodes. If those 3 attributes are equal, i need to replace the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.