473,799 Members | 3,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="For m1" componente="btn Ok" traducao="" id="-1">
<item>0</item>
<comp_Text>Ok </comp_Text>
</comp>
<comp formulario="For m1" componente="lbl Form1" traducao="" id="-1">
<item>1</item>
<comp_Text>Form ulário 1</comp_Text>
</comp>
</Traducao>

File 2:
<?xml version="1.0" standalone="yes " ?>
<Traducao xmlns="http://www.tempuri.org/traducao.xsd">
<comp formulario="For m1" componente="btn Ok" traducao="Ok" id="-1">
<item>0</item>
<comp_Text>Ok </comp_Text>
</comp>
<comp formulario="For m1" componente="lbl Form1" traducao="Form 1" id="-1">
<item>1</item>
<comp_Text>Form ulá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 3343
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.Prima ryKey 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.Ad ded, which is why Merge is adding
the rows to the first DataTable, not replacing them.

Unfortunately, it is not possible to manipulate DataRow.RowStat e
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[] table2KeyColumn s = table2.PrimaryK ey;
object[] row2Key = new object[table2KeyColumn s.Length];
foreach (DataRow row2 in table2)
{
foreach (int c = 0; c < table2KeyColumn s.Length; c++)
{
row2Key[c] = row2[table2KeyColumn s[c]];
}
DataRow row1 = table1.Rows.Fin d(row2Key);
if (row1 == null)
{
table1.ImportRo w(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.Prima ryKey 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.Ad ded, which is why Merge is adding
the rows to the first DataTable, not replacing them.

Unfortunately, it is not possible to manipulate DataRow.RowStat e
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[] table2KeyColumn s = table2.PrimaryK ey;
object[] row2Key = new object[table2KeyColumn s.Length];
foreach (DataRow row2 in table2)
{
foreach (int c = 0; c < table2KeyColumn s.Length; c++)
{
row2Key[c] = row2[table2KeyColumn s[c]];
}
DataRow row1 = table1.Rows.Fin d(row2Key);
if (row1 == null)
{
table1.ImportRo w(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
2937
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 merge the two, into, say, a BranchC somehow on occassion? Thanks, Ike
1
1539
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 the encoding we are using. We don't specify a type and I think it's defaulting to a 7-bit encoding, when I think we need 8. First of all, does that sound right? If so, does anyone have an example of how to specify the right encoding for PDF...
3
11021
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 like to merge both datasets in such a way that the resulting dataset should have all the today's data overriding yestrerday's data. CATCH: the today's dataset contains only the daily changes (DELTA) and merging should not remove all unchanged...
0
1376
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 dataset should have all the today's data overriding yestrerday's data. As you may guess, the today's dataset contains only the daily changes and merging should not remove all unchanged records. What's the most efficient way of doing so ? I...
0
1244
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 of code in the ascx file Original is <%@ Register TagPrefix="uc1" TagName="LogonMessageDisplay" Src="../../Controls/LogonMessageDisplay.ascx" %>
12
1425
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 duplicates. And to make it harder (or not???!!) my criteria for defining the duplicate is the left 15 (or so) characters of the file path. Help, as always, is greatly appreciated!
3
1969
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 the correct node. Any non conflicting differences should be automatically merge onto the merge document. Any suggestion/caveats on how to do this in VB.Net? Regards Sanjib doc1.xml |doc2.xml ======== ...
1
4841
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 fourth columns in my table, but in the second column, the merged cell is not aligned at the center vertically. The cells do get merged, but the cells in the second column are always aligned at the top. Trying cell.valign = "middle" had no effect. ...
3
3879
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
1751
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 data that was created from .txt delimited files I have received. I tried the following code. SELECT VIN, OtherID, Rego, DriverName, OwnerName, VehicleDescription, ExpiryDate, Address, City, Postcode, State INTO Iload FROM STDhy60, STDhy59,...
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.