compare 2 xml files with csharp

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Mar 2006
Posts: 31
Reputation: Yustme is an unknown quantity at this point 
Solved Threads: 0
Yustme Yustme is offline Offline
Light Poster

compare 2 xml files with csharp

 
0
  #1
May 25th, 2006
Hi,

Im having a lil problem which i can't figure out myself.. I tried searching google, but i found nothing what might even help me the slightest bit..

I wanna compare 2 xml files with eachother. One xml file is standard on my pc and has the name pc.xml. the other xml file is on the server and has the name server.xml

The program im trying to write should compare these 2 files with eachother and if it contains new info it should be added in the pc.xml file. and the server.xml files should be disposed after this is done so a new file can be send without getting an error about having 2 xml files with the same name on my pc.


This is the pc.xml file:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <agenda>
  3. <appointment>
  4. <Id> 7 </Id>
  5. <Date> 20060426 </Datum>
  6. <Time> 120000 </Time>
  7. <Subject> test </Subject>
  8. <Description> testttt </Beschrijving>
  9. <Place> paris </Place>
  10. <Modtime> 182204 </Modtime>
  11. </Appointment>
  12. <Appointment>
  13. <Id> 8 </Id>
  14. <Date> 20060426 </Date>
  15. <Time> 120000 </Time>
  16. <Subject> whatever </Subject>
  17. <Description> whateverrr </Description>
  18. <Place> where ever </Place>
  19. <Modtime> 182204 </Modtime>
  20. </Appointment>
  21. </agenda>


And this one will be send from the server:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <agenda>
  3. <appointment>
  4. <Id> 9 </Id>
  5. <Date> 20060426 </Datum>
  6. <Time> 120000 </Time>
  7. <Subject> test </Subject>
  8. <Description> testttt </Beschrijving>
  9. <Place> paris </Place>
  10. <Modtime> 182204 </Modtime>
  11. </Appointment>
  12. <Appointment>
  13. <Id> 10 </Id>
  14. <Date> 20060426 </Date>
  15. <Time> 120000 </Time>
  16. <Subject> whatever </Subject>
  17. <Description> whateverrr </Description>
  18. <Place> where ever </Place>
  19. <Modtime> 182204 </Modtime>
  20. </Appointment>
  21. </agenda>


As you can see by looking at the Id's of these 2 xml files, they are different. There is id 7, 8, 9 and 10.

I want this file merge with the excisting one on my pc (pc.xml). After merged it should look like this:


  1. <agenda>
  2. <appointment>
  3. <Id> 7 </Id>
  4. <Date> 20060426 </Datum>
  5. <Time> 120000 </Time>
  6. <Subject> test </Subject>
  7. <Description> testttt </Beschrijving>
  8. <Place> paris </Place>
  9. <Modtime> 182204 </Modtime>
  10. </Appointment>
  11. <Appointment>
  12. <Id> 8 </Id>
  13. <Date> 20060426 </Date>
  14. <Time> 120000 </Time>
  15. <Subject> whatever </Subject>
  16. <Description> whateverrr </Description>
  17. <Place> where ever </Place>
  18. <Modtime> 182204 </Modtime>
  19. </Appointment>
  20. <appointment>
  21. <Id> 9 </Id>
  22. <Date> 20060426 </Datum>
  23. <Time> 120000 </Time>
  24. <Subject> test </Subject>
  25. <Description> testttt </Beschrijving>
  26. <Place> paris </Place>
  27. <Modtime> 182204 </Modtime>
  28. </Appointment>
  29. <Appointment>
  30. <Id> 10 </Id>
  31. <Date> 20060426 </Date>
  32. <Time> 120000 </Time>
  33. <Subject> whatever </Subject>
  34. <Description> whateverrr </Description>
  35. <Place> where ever </Place>
  36. <Modtime> 182204 </Modtime>
  37. </Appointment>
  38. </agenda>

I can't find an example on the internet how this can be done in csharp. Nor can i find if its possible to compare the content of 2 xml files with eachother...

Can somebody help me with this please?

Thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: compare 2 xml files with csharp

 
0
  #2
May 25th, 2006
create a class called appointment with the appropriate fields.
create an array of appointments using your existing pc.xml
read in the new one and compare each new one to the ones in your array.
if it has the same id as one of the array items, compare the data and update the array item data with the data from the new xml
if it is a new id,
add it to the end of the array

after you have gone through the new xml, your array should have the combined data

iterate through the array and write to the file
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: compare 2 xml files with csharp

 
0
  #3
May 26th, 2006
an even easier way, just use/do the following.

1.
read in the xml files with
DataSet.ReadXml method

2.
compare the id columns in the dataset to each other, looking for duplicates or whatever else. I think this can be done very easy with a dataview, ive only done a small amout of work with a dataview so im not to sure.

3.
modify the dataset to contain all the new info

4.
delete old xml file

5.
write the new xml file with using the dataset
DataSet.WriteXml method
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: compare 2 xml files with csharp

 
0
  #4
May 26th, 2006
well sure, if you want to do it the easy way
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 31
Reputation: Yustme is an unknown quantity at this point 
Solved Threads: 0
Yustme Yustme is offline Offline
Light Poster

Re: compare 2 xml files with csharp

 
0
  #5
May 26th, 2006
Originally Posted by plazmo
an even easier way, just use/do the following.

1.
read in the xml files with
DataSet.ReadXml method

2.
compare the id columns in the dataset to each other, looking for duplicates or whatever else. I think this can be done very easy with a dataview, ive only done a small amout of work with a dataview so im not to sure.

3.
modify the dataset to contain all the new info

4.
delete old xml file

5.
write the new xml file with using the dataset
DataSet.WriteXml method


Hi plazmo,

Thank you for your reply.

Its too difficult for me to understand what you all said there.

Im new to c sharp. Can you show me some example codes please?

Thnx in advance!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: compare 2 xml files with csharp

 
0
  #6
May 26th, 2006
hopeing you understand this!
dataset contains an array of tables
the tables contain an array of row which are your records
and the table contains an array of columns

you can call the rows by number then get the value of the rows column by useing the colums name or order number
ex.
gets the data from the first table in the first row of the "Id" column
myDataSet.Table[0].Rows[1]["Id"]

there is quite a lot to datasets and i believe it is one of the most imporant things to know because it will be uses in most if not all your projects which involve databases.

  1.  
  2. private void PlazmosUberXML11111(){
  3. DataSet serverXML = new DataSet("serverXML"); //server data
  4. DataSet pcXML = new DataSet("pcXML"); //pc data
  5. DataSet outputXML = new DataSet("Output"); //output
  6.  
  7. //read in our files
  8. try
  9. {
  10. serverXML.ReadXml("XMLFile.xml", XmlReadMode.Auto);
  11. pcXML.ReadXml("XMLFile2.xml", XmlReadMode.Auto);
  12. }
  13. catch(Exception ex)
  14. {
  15. MessageBox.Show(ex.Message);
  16. }
  17.  
  18. //output is server plus new records from pc i think???????
  19. //so start with server then add the new pc records
  20. outputXML = serverXML;
  21.  
  22.  
  23. //compare each record of the pcs xml to the servers, and see if the server is missing any
  24. //we only need to look at the first table of the dataset because that is where the xml gets read into
  25. foreach(DataRow pcRow in pcXML.Tables[0].Rows)
  26. {
  27. bool hasRow = false;
  28. foreach(DataRow serverRow in serverXML.Tables[0].Rows)
  29. {
  30. //this looks at only the column named "Id" in our records
  31. //if we find a matching set bool and break out of loop
  32. if(pcRow["Id"].ToString() == serverRow["Id"].ToString())
  33. {
  34. hasRow = true;
  35. break;
  36. }
  37.  
  38. }
  39.  
  40. if(hasRow)
  41. {
  42. //do something if your finding simliar rows
  43. }
  44. else //do something if your finding a row it doesnt have
  45. {
  46. DataRow newRow = outputXML.Tables[0].NewRow(); //make a new row with the tables scheme
  47. newRow.ItemArray = pcRow.ItemArray; //copy over the contents of the row
  48. outputXML.Tables[0].Rows.Add(newRow); //add the new row to our final table
  49. }
  50. }
  51.  
  52. //write it to the xml file
  53. outputXML.WriteXml("newXML.xml");
  54.  
  55. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 31
Reputation: Yustme is an unknown quantity at this point 
Solved Threads: 0
Yustme Yustme is offline Offline
Light Poster

Re: compare 2 xml files with csharp

 
0
  #7
May 27th, 2006
Originally Posted by plazmo
hopeing you understand this!
dataset contains an array of tables
the tables contain an array of row which are your records
and the table contains an array of columns

you can call the rows by number then get the value of the rows column by useing the colums name or order number
ex.
gets the data from the first table in the first row of the "Id" column
myDataSet.Table[0].Rows[1]["Id"]

there is quite a lot to datasets and i believe it is one of the most imporant things to know because it will be uses in most if not all your projects which involve databases.

  1.  
  2. private void PlazmosUberXML11111(){
  3. DataSet serverXML = new DataSet("serverXML"); //server data
  4. DataSet pcXML = new DataSet("pcXML"); //pc data
  5. DataSet outputXML = new DataSet("Output"); //output
  6.  
  7. //read in our files
  8. try
  9. {
  10. serverXML.ReadXml("XMLFile.xml", XmlReadMode.Auto);
  11. pcXML.ReadXml("XMLFile2.xml", XmlReadMode.Auto);
  12. }
  13. catch(Exception ex)
  14. {
  15. MessageBox.Show(ex.Message);
  16. }
  17.  
  18. //output is server plus new records from pc i think???????
  19. //so start with server then add the new pc records
  20. outputXML = serverXML;
  21.  
  22.  
  23. //compare each record of the pcs xml to the servers, and see if the server is missing any
  24. //we only need to look at the first table of the dataset because that is where the xml gets read into
  25. foreach(DataRow pcRow in pcXML.Tables[0].Rows)
  26. {
  27. bool hasRow = false;
  28. foreach(DataRow serverRow in serverXML.Tables[0].Rows)
  29. {
  30. //this looks at only the column named "Id" in our records
  31. //if we find a matching set bool and break out of loop
  32. if(pcRow["Id"].ToString() == serverRow["Id"].ToString())
  33. {
  34. hasRow = true;
  35. break;
  36. }
  37.  
  38. }
  39.  
  40. if(hasRow)
  41. {
  42. //do something if your finding simliar rows
  43. }
  44. else //do something if your finding a row it doesnt have
  45. {
  46. DataRow newRow = outputXML.Tables[0].NewRow(); //make a new row with the tables scheme
  47. newRow.ItemArray = pcRow.ItemArray; //copy over the contents of the row
  48. outputXML.Tables[0].Rows.Add(newRow); //add the new row to our final table
  49. }
  50. }
  51.  
  52. //write it to the xml file
  53. outputXML.WriteXml("newXML.xml");
  54.  
  55. }


Hi plazmo,

Thank you for the time you took to post this. I really appreciate it!

Im trying to figure out what this code does. Most of it i understand coz of your comments.

Im getting an error when i execute the program. It cant find the file, but when i follow the path it gives me in the console. I see its there.

When i debug it, it tells me that Tables[0] is out of reach in this line:

foreach(DataRow pcRow in pcXML.Tables[0].Rows)

And another thing i wanna know is where do i put this:

myDataSet.Table[0].Rows[1]["Id"]


Should i change the names and path for the XMLFiles1 and 2 in these 2 lines:

serverXML.ReadXml("XMLFile.xml", XmlReadMode.Auto);
pcXML.ReadXml("XMLFile2.xml", XmlReadMode.Auto);


I thank you very much plazmo!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: compare 2 xml files with csharp

 
0
  #8
May 27th, 2006
oh yeah a few things i forgot to mention
the code you posted was not formatted properly.
a few tags were spelled differently then they were closing, like:
<Date> 20060426 </Datum>

<Description> testttt </Beschrijving>

and
when you are debugging a program, the files need to be in under \bin\Debug\ of the project folder, that is actually where it is getting executed. that is if you wanted to only use the file name, otherwise put in the full file path



myDataSet.Table[0].Rows[1]["Id"]
doesnt need to be anywhere i was jsut trying to explain how a dataset works
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 31
Reputation: Yustme is an unknown quantity at this point 
Solved Threads: 0
Yustme Yustme is offline Offline
Light Poster

Re: compare 2 xml files with csharp

 
0
  #9
May 27th, 2006
Originally Posted by plazmo
oh yeah a few things i forgot to mention
the code you posted was not formatted properly.
a few tags were spelled differently then they were closing, like:
<Date> 20060426 </Datum>

<Description> testttt </Beschrijving>

and
when you are debugging a program, the files need to be in under \bin\Debug\ of the project folder, that is actually where it is getting executed. that is if you wanted to only use the file name, otherwise put in the full file path



myDataSet.Table[0].Rows[1]["Id"]
doesnt need to be anywhere i was jsut trying to explain how a dataset works

Yea those tags where not all translated, i forgot a few, but in the xml doc they are fine

I found the problem why it didn't find the docs. We forgot to put the .xml extention behind those doc names.

The program works fine, thank you plazmo

The next step im trying to do is when the Id's matches eachother from those 2 files, it should check what is changed, and clone the changed things in the pcXML doc.

Is it possible to clone tags with their changed elements from file to file?

Thnx in advance!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: compare 2 xml files with csharp

 
0
  #10
May 27th, 2006
sure just look at the rows itemarray and check what has changed

like do this after you find a matching id:
for(int i=0;i<pcRow.ItemArray.Length;i++){
if(pcRow.ItemArray[i] != serverRow.ItemArray[i])
{then this item is different in this column of the row}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC