473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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?
Apr 17 '06 #1
2 3326
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.

Apr 17 '06 #2
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.
"Bruce Wood" wrote:
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.

Apr 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ike | last post by:
Suppose I have a relational database, called "BranchA." I have a second relational database, of identical structure to BranchA, but with different data, and this one is called BranchB. Can I...
1
by: Jeff | last post by:
We're having some trouble with some PDF files sent to our customers, they aren't able to be read because they are "damaged, and cannot be repaired". Looking into this, I think it has to do with...
3
by: Mike | last post by:
Hi! I also asked this question in C# group with no results: I have 2 datasets loaded with data from two xml files having the same schema. The files contain data from yesterday and today. I'd...
0
by: Mike | last post by:
Hi! I have 2 datasets loaded with data from two xml files having the same schema. The files contain data from yesterday and today. I'd like to merge both datasets in such a way that the resulting...
0
by: Eric Mamet | last post by:
Everytime I try to look at a web user control in design mode, Visual Studio prompts me for checking out the file from Visual Source Safe. Apparently, it just shuffles around the first three lines...
12
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the...
3
by: Sanjib Biswas | last post by:
Hi All, I am looking for XML merging for the following scenarios. I want to load both the input files and show in the tree viewer and highlight the differences. Now its up to the user to select...
1
by: samn | last post by:
I wrote the following script in order to traverse an HTML table and merge the cells that have the same value across multiple rows. For some reason, however, it works for the first, third, and...
3
by: Ralph Smith | last post by:
I have two identical databases on two different servers and I need to add the data in tables from one server to the tables in the other server. Is there a way to do that in mysql? thanks, Ralph
1
by: Big X | last post by:
I have already achieved this in access and was trying with straight SQL earlier I would just like to know what I'm doing wrong in sql or what syntax I'm missing. I have three tables with identical...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.