Merging XML Not Working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I know there is a lot of talk on here about merging, but I just can't seem to
get the DataSet.Merge to work with XML data. My scenario is that I have a
DataSet populated with complex XML. Every so often on a timed interval I get
sent to me a new set of XML (could have changes, removed or added
information). I want to add (merge) that XML with the current DataSet.

I have been working on this for over 1.5 days and can't seem to get it to
work properly. The merged data does not come out correctly. I have read just
about everything on the web and news groups with no luck. The XML that I am
working with is pretty complex and really does not have any primary keys
except for maybe one element (table).

So I decided to try some simpler XML to see if I could get it working. Below
are two "simple" xml files that I am using to test the DataSet.Merge:

File 1:
<CDs>
<CD id="5">
<Name>A Perfect Circle</Name>
<Price>10</Price>
<RecordCompany id="1">
<Name>APC Records</Name>
</RecordCompany>
</CD>
<CD id="6">
<Name>Sevendust</Name>
<Price>10</Price>
<RecordCompany id="2">
<Name>TVT Records</Name>
</RecordCompany>
</CD>
</CDs>

File 2:
<CDs>
<CD id="1">
<Name>AC/DC</Name>
<Price>10</Price>
<RecordCompany id="3">
<Name>Epic</Name>
</RecordCompany>
</CD>
<CD id="3">
<Name>Cheap Trick</Name>
<Price>11</Price>
<RecordCompany id="4">
<Name>Ant Records</Name>
</RecordCompany>
</CD>
</CDs>

When I merge these two files (I have tried many different ways) I get these
results:

<CDs>
<CD id="5">
<Name>A Perfect Circle</Name>
<Price>10</Price>
<RecordCompany id="3">
<Name>Epic</Name>
</RecordCompany>
<RecordCompany id="1">
<Name>APC Records</Name>
</RecordCompany>
</CD>
<CD id="6">
<Name>Sevendust</Name>
<Price>10</Price>
<RecordCompany id="4">
<Name>Ant Records</Name>
</RecordCompany>
<RecordCompany id="2">
<Name>TVT Records</Name>
</RecordCompany>
</CD>
</CDs>

As you can see, two of the "CD" nodes are completely missing and the
"RecordCompany" nodes are mixed in with the wrong CD. This is similar to what
is happening with my XML.

Any ideas?

Thanks,
David McCarter


=====================
David McCarter
www.vsdntips.com
 
Assuming that

File1 is loaded into ds1.
File2 is loaded into ds2.

If you do a ds1.Merge(ds2) - the keys in ds1 will OVERRIDE ds2's.

In your example below - once you've loaded stuff into the datasets, the CD
IDs are loaded as 0 and 1 in either case.
So when you do a ds1.Merge(ds2), the merge logic assumes that ds1 represnts
the values that must OVERRIDE ds2's values and thus knocks out two CDs.

Logical question - "How can I use the IDs I specified?" - by specifying a
proper schema before you do a ReadXml so the dataset knows how to interpret
your XML data.

HTH


- Sahil Malik
http://codebetter.com/blogs/sahil.malik/
 
Back
Top