Merging XML files with identical schemas in C# ? How to?

G

Guest

I'm trying to use DataSet.Merge, but it works like an Append, and I don't
want this. I want to update dataset data.

I have two files with same fields and the same xml schema. For example:

File 1:
<?xml version="1.0" standalone="yes" ?>
<Traducao xmlns="http://www.tempuri.org/traducao.xsd">
<comp formulario="Form1" componente="btnOk" traducao="" id="-1">
<item>0</item>
<comp_Text>Ok</comp_Text>
</comp>
<comp formulario="Form1" componente="lblForm1" traducao="" id="-1">
<item>1</item>
<comp_Text>Formulário 1</comp_Text>
</comp>
</Traducao>

File 2:
<?xml version="1.0" standalone="yes" ?>
<Traducao xmlns="http://www.tempuri.org/traducao.xsd">
<comp formulario="Form1" componente="btnOk" traducao="Ok" id="-1">
<item>0</item>
<comp_Text>Ok</comp_Text>
</comp>
<comp formulario="Form1" componente="lblForm1" traducao="Form 1" id="-1">
<item>1</item>
<comp_Text>Formulário 1</comp_Text>
</comp>
</Traducao>

The difference between them, as you can see, is the attribute "traducaoo",
wich has value in the second file.

A intenção é pergar o dataset (que contem o arquivo1) e unir ao dataset2
(que contem o segundo arquivo), apenas atualizando os dados do primeiro
dataset - sem fazer append.

I want to take the first dataset (with contains the first file), and merge
with the second dataset (wich contains the second file), just updating the
registries - not appending them.

Can anyone help me?
 
B

Bruce Wood

I haven't done this myself, but reading the documentation, it appears
that you require two things in order to make this work properly.

First, your two DataTables need primary keys set. So, you must use
DataTable.PrimaryKey to tell ADO.NET which column or columns in your
tables uniquely identify the rows. Both tables must have the same
primary key column or columns.

Second, the second DataTable, read from XML, must have the RowState of
each row set to Modified. From your description, I assume that they are
all currently set to DataRowState.Added, which is why Merge is adding
the rows to the first DataTable, not replacing them.

Unfortunately, it is not possible to manipulate DataRow.RowState
directly, and I can't see any easy way to change it to Modified. (It's
easy to change it from Added to Unchanged: just call AcceptChanges on
the row. Then changing it to Modified is a different matter.)

Perhaps using DataSet.Merge is not the right way to approach this
problem. Perhaps it would be better to code it yourself, something like
this:

// Set PrimaryKey for both table1 and table2.
DataColumn[] table2KeyColumns = table2.PrimaryKey;
object[] row2Key = new object[table2KeyColumns.Length];
foreach (DataRow row2 in table2)
{
foreach (int c = 0; c < table2KeyColumns.Length; c++)
{
row2Key[c] = row2[table2KeyColumns[c]];
}
DataRow row1 = table1.Rows.Find(row2Key);
if (row1 == null)
{
table1.ImportRow(row2);
}
else
{
foreach (DataColumn col2 in table2.Columns)
{
if (!row1[col2.Name].Equals(row2[col2]))
{
row1[col2.Name] = row2[col2];
}
}
}
}

Yes, it's tedious code, but at least you have total control, and can
introduce refinements such as not checking certain columns, or updating
only if the value in table1 is missing, etc.

Merge looks nice, but in my opinion it's designed to do something
different from what you want to do.
 
G

Guest

Hi Bruce,

Thanks for your answer. You are right about the RowState.

I've done it the way you said (searching and matching rows).

I guess it would be slow on big files, but this software is for eventual
use, so it's not a big trouble.

Thanks again.
 

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