Monday, May 17, 2010

compare 2 xml files with csharp




7
20060426

test
testttt
paris
182204


8
20060426

whatever
whateverrr
where ever
182204







9
20060426

test
testttt
paris
182204


10
20060426

whatever
whateverrr
where ever
182204







7
20060426

test
testttt
paris
182204


8
20060426

whatever
whateverrr
where ever
182204


9
20060426

test
testttt
paris
182204


10
20060426

whatever
whateverrr
where ever
182204




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

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

}

http://www.daniweb.com/forums/thread46345.html#

No comments: