By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,678 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,678 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 records from yesterday.

What's the most efficient way of doing so ? I thought ds.Merge() method
would work for me, but I can't make it work. It seems that I can append two
datasets but not merge
ds1.Merge(ds2,false)

Many Thanks in Advance,

Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Mike

I'm pretty sure that the behaviour you are after is the default behaviour.
You need to have set up the primary keys on the table so that updated rows
can be identified. The only thing I've found that isn't mapped through is
deletions - but new and changed data is merged.

Is that not what you are finding?

Nigel

"Mike" wrote:
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 records from yesterday.

What's the most efficient way of doing so ? I thought ds.Merge() method
would work for me, but I can't make it work. It seems that I can append two
datasets but not merge
ds1.Merge(ds2,false)

Many Thanks in Advance,

Nov 12 '05 #2

P: n/a
Hi! Thank you for taking a look. I am populating my datasets from XML files.
I have no clue how to set up the primary keys on the table. Perhaps you can
point me in the right direction ? Without primary keys set, I saw 2 datasets
just appending all data to each other.

Many Thanks

--Mike

"Nigel Armstrong" wrote:
Hi Mike

I'm pretty sure that the behaviour you are after is the default behaviour.
You need to have set up the primary keys on the table so that updated rows
can be identified. The only thing I've found that isn't mapped through is
deletions - but new and changed data is merged.

Is that not what you are finding?

Nigel

"Mike" wrote:
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 records from yesterday.

What's the most efficient way of doing so ? I thought ds.Merge() method
would work for me, but I can't make it work. It seems that I can append two
datasets but not merge
ds1.Merge(ds2,false)

Many Thanks in Advance,

Nov 12 '05 #3

P: n/a
Hi Mike

Files follow. We have Stuff.xsd, the schema file. Run this through the xsd
utility using the /dataset option to generate the DataSet classes.

Stuff.xsd
<?xml version="1.0" standalone="yes"?>
<xs:schema id="Stuff" targetNamespace="http://www.tempuri.org/Stuff.xsd"
xmlns:mstns="http://www.tempuri.org/Stuff.xsd"
xmlns="http://www.tempuri.org/Stuff.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="Stuff" msdata:IsDataSet="true" msdata:Locale="en-GB">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="id" msdata:ReadOnly="true"
msdata:AutoIncrement="true" type="xs:int" />
<xs:element name="Food" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:Table" />
<xs:field xpath="mstns:id" />
</xs:unique>
</xs:element>
</xs:schema>

Two xml files, base.xml the data as it is now and delta.xml, the changes.

base.xml
<?xml version="1.0" standalone="yes"?>
<Stuff xmlns="http://www.tempuri.org/Stuff.xsd">
<Table>
<id>1</id>
<Food>Apple</Food>
</Table>
<Table>
<id>2</id>
<Food>Banana</Food>
</Table>
<Table>
<id>3</id>
<Food>Carrot</Food>
</Table>
<Table>
<id>4</id>
<Food>Doughnut</Food>
</Table>
<Table>
<id>5</id>
<Food>Egg</Food>
</Table>
</Stuff>

delta.xml
<?xml version="1.0" standalone="yes"?>
<Stuff xmlns="http://www.tempuri.org/Stuff.xsd">
<Table>
<id>2</id>
<Food>Bananas</Food>
</Table>
<Table>
<id>5</id>
<Food>Eggs</Food>
</Table>
<Table>
<id>7</id>
<Food>Grits</Food>
</Table>
<Table>
<id>8</id>
<Food>Hash Browns</Food>
</Table>
</Stuff>

Finally some code. Load up base.xml, then merge it with delta.xml

Stuff s = new Stuff();
s.ReadXml(@"C:\base.xml");
MessageBox.Show(s.GetXml());
Stuff s2 = new Stuff();
s2.ReadXml(@"C:\delta.xml");
MessageBox.Show(s2.GetXml());
s.Merge(s2);
MessageBox.Show(s.GetXml());

This works for me...

HTH

Nigel

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.