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:

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

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

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

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

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

well sure, if you want to do it the easy way ;)

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.

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");
			
		}

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.

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

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!

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 != serverRow.ItemArray)
{then this item is different in this column of the row}
}

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 != serverRow.ItemArray)
{then this item is different in this column of the row}
}

If i found the item which is different from the pcXML doc, how can i make it copy or clone to from serverXML to pcXML?

I tried this:

serverRow.ItemArray.Clone(outputXML);

its correct code, no errors, but it doesnt work. I have no idea how its possible to clone from file to file.

Also i keep seeing this in the console when i execute this program:

'Collection was modified; enumeration operation might not execute'

What does that mean?

Thanks in advance!

look at what i did to copy a different row!

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);

you make a row object with the same shceme as the table then copy to source itemarray to the new row, then append the row to the talbe

look at what i did to copy a different row!

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);

you make a row object with the same shceme as the table then copy to source itemarray to the new row, then append the row to the talbe

Oh ofcourse, stupid me. Im sorry, its just that i worked on this for like a week fulltime.

I can't think straight anymore :eek:

Thanks plazmo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.