| | |
compare 2 xml files with csharp
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Mar 2006
Posts: 31
Reputation:
Solved Threads: 0
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:
And this one will be send from the server:
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:
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!
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:
C# Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="ISO-8859-1"?> <agenda> <appointment> <Id> 7 </Id> <Date> 20060426 </Datum> <Time> 120000 </Time> <Subject> test </Subject> <Description> testttt </Beschrijving> <Place> paris </Place> <Modtime> 182204 </Modtime> </Appointment> <Appointment> <Id> 8 </Id> <Date> 20060426 </Date> <Time> 120000 </Time> <Subject> whatever </Subject> <Description> whateverrr </Description> <Place> where ever </Place> <Modtime> 182204 </Modtime> </Appointment> </agenda>
And this one will be send from the server:
C# Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="ISO-8859-1"?> <agenda> <appointment> <Id> 9 </Id> <Date> 20060426 </Datum> <Time> 120000 </Time> <Subject> test </Subject> <Description> testttt </Beschrijving> <Place> paris </Place> <Modtime> 182204 </Modtime> </Appointment> <Appointment> <Id> 10 </Id> <Date> 20060426 </Date> <Time> 120000 </Time> <Subject> whatever </Subject> <Description> whateverrr </Description> <Place> where ever </Place> <Modtime> 182204 </Modtime> </Appointment> </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:
C# Syntax (Toggle Plain Text)
<agenda> <appointment> <Id> 7 </Id> <Date> 20060426 </Datum> <Time> 120000 </Time> <Subject> test </Subject> <Description> testttt </Beschrijving> <Place> paris </Place> <Modtime> 182204 </Modtime> </Appointment> <Appointment> <Id> 8 </Id> <Date> 20060426 </Date> <Time> 120000 </Time> <Subject> whatever </Subject> <Description> whateverrr </Description> <Place> where ever </Place> <Modtime> 182204 </Modtime> </Appointment> <appointment> <Id> 9 </Id> <Date> 20060426 </Datum> <Time> 120000 </Time> <Subject> test </Subject> <Description> testttt </Beschrijving> <Place> paris </Place> <Modtime> 182204 </Modtime> </Appointment> <Appointment> <Id> 10 </Id> <Date> 20060426 </Date> <Time> 120000 </Time> <Subject> whatever </Subject> <Description> whateverrr </Description> <Place> where ever </Place> <Modtime> 182204 </Modtime> </Appointment> </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!
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
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
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
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
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
•
•
Join Date: Mar 2006
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
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!
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.
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.
C# Syntax (Toggle Plain Text)
private void PlazmosUberXML11111(){ DataSet serverXML = new DataSet("serverXML"); //server data DataSet pcXML = new DataSet("pcXML"); //pc data DataSet outputXML = new DataSet("Output"); //output //read in our files try { serverXML.ReadXml("XMLFile.xml", XmlReadMode.Auto); pcXML.ReadXml("XMLFile2.xml", XmlReadMode.Auto); } catch(Exception ex) { MessageBox.Show(ex.Message); } //output is server plus new records from pc i think??????? //so start with server then add the new pc records outputXML = serverXML; //compare each record of the pcs xml to the servers, and see if the server is missing any //we only need to look at the first table of the dataset because that is where the xml gets read into foreach(DataRow pcRow in pcXML.Tables[0].Rows) { bool hasRow = false; foreach(DataRow serverRow in serverXML.Tables[0].Rows) { //this looks at only the column named "Id" in our records //if we find a matching set bool and break out of loop if(pcRow["Id"].ToString() == serverRow["Id"].ToString()) { hasRow = true; break; } } if(hasRow) { //do something if your finding simliar rows } else //do something if your finding a row it doesnt have { DataRow newRow = outputXML.Tables[0].NewRow(); //make a new row with the tables scheme newRow.ItemArray = pcRow.ItemArray; //copy over the contents of the row outputXML.Tables[0].Rows.Add(newRow); //add the new row to our final table } } //write it to the xml file outputXML.WriteXml("newXML.xml"); }
•
•
Join Date: Mar 2006
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
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.
C# Syntax (Toggle Plain Text)
private void PlazmosUberXML11111(){ DataSet serverXML = new DataSet("serverXML"); //server data DataSet pcXML = new DataSet("pcXML"); //pc data DataSet outputXML = new DataSet("Output"); //output //read in our files try { serverXML.ReadXml("XMLFile.xml", XmlReadMode.Auto); pcXML.ReadXml("XMLFile2.xml", XmlReadMode.Auto); } catch(Exception ex) { MessageBox.Show(ex.Message); } //output is server plus new records from pc i think??????? //so start with server then add the new pc records outputXML = serverXML; //compare each record of the pcs xml to the servers, and see if the server is missing any //we only need to look at the first table of the dataset because that is where the xml gets read into foreach(DataRow pcRow in pcXML.Tables[0].Rows) { bool hasRow = false; foreach(DataRow serverRow in serverXML.Tables[0].Rows) { //this looks at only the column named "Id" in our records //if we find a matching set bool and break out of loop if(pcRow["Id"].ToString() == serverRow["Id"].ToString()) { hasRow = true; break; } } if(hasRow) { //do something if your finding simliar rows } else //do something if your finding a row it doesnt have { DataRow newRow = outputXML.Tables[0].NewRow(); //make a new row with the tables scheme newRow.ItemArray = pcRow.ItemArray; //copy over the contents of the row outputXML.Tables[0].Rows.Add(newRow); //add the new row to our final table } } //write it to the xml file outputXML.WriteXml("newXML.xml"); }
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!
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
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
•
•
Join Date: Mar 2006
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
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!
![]() |
Similar Threads
- Java code to compare XML files (Java)
- How to compare folders of XML files in Perl using ExamXML (Perl)
Other Threads in the C# Forum
- Previous Thread: FolderBrowserDialog problems
- Next Thread: Exporting Data from App To Excel c#
| Thread Tools | Search this Thread |
.net access algorithm animation array bitmap box c# check checkbox client combobox control conversion csharp customactiondata cyclethruopenforms data database datagrid datagridview dataset date/time datetime datetimepicker degrees directrobot dll draganddrop drawing encryption enum excel file filename files finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization gtk hash image input install installer java list math mono mouseclick mysql operator outlook2003 panel path photoshop picturebox pixelinversion pixelminversion post print programming radians regex remoting richtextbox save server sleep snooze socket sql sql-server statistics string table tables tcp text textbox thread time timer timespan update usercontrol usercontrols users validate validation visualstudio webcam wia wpf xml





