472,101 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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
3 10875
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
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
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.

Similar topics

15 posts views Thread by Ike | last post: by
1 post views Thread by Jeff | last post: by
2 posts views Thread by Thanya Teutschbeim | last post: by
3 posts views Thread by Sanjib Biswas | last post: by
3 posts views Thread by Ralph Smith | last post: by

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.