Dataset.Merge - troubles

S

ScottDizzy

I'm trying to load an xml file into a dataset, then load the same xml
file with a slight change into a second dataset, then do a merge to
see what changed between datasets.


DataSet xmlDataSet = new DataSet("Original");
xmlDataSet.ReadXml("C:\\temp\\a.xml");
xmlDataSet.AcceptChanges();
PrintValues(xmlDataSet, "Original");

DataSet xml2 = new DataSet("New");
xml2.ReadXml("C:\\temp\\b.xml");
xml2.AcceptChanges();
PrintValues(xml2, "New");

xmlDataSet.Merge(xml2);

DataSet DSChanges = new DataSet("change");;
DSChanges = xmlDataSet.GetChanges();

DSChanges.AcceptChanges();
PrintValues(DSChanges, "Changes");

public static void PrintValues(DataSet ds, string label)
{
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables)
{
Console.WriteLine("TableName: " +
t.TableName);
foreach(DataRow r in t.Rows)
{
foreach(DataColumn c in t.Columns)
{

Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}

XML file A:
<?xml version="1.0"?>
<sheet title="mytitle">
<section title="General"></section>
<section title="Processor"></section>
</sheet>

XML file B:
<?xml version="1.0"?>
<sheet title="mytitle">
<section title="General1"></section>
<section title="Processor"></section>
</sheet>


I'm doing an Acceptchanges after reading both files to change the row
state to unchanged instead of added. Because if I didn't when I do
the merge, all rows would be Added. I just want to pull the
changes. Pretty much comparing the two datasets and getting the
differences.

Thanks for your help.
 
C

Cor Ligthert

Hi Scott,

As far as I know. To do a merge in a dataset where the datarows of the
tablew will be merged, the rows needs to have a primary key.

And there may be now rows with the same key in one of the two tables.

It does not know what rows should stay. When you want to do such a kind of
merge, you can create a looping routine which tells which rows have to be
where (when you do that look for the importrow method, because that saves a
lot of work).

http://msdn.microsoft.com/library/d...lrfsystemdatadatatableclassimportrowtopic.asp

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top