help with reading xml file

D

davis

Hello, I am working in .Net C# and have an xml file similar to the one
below. I have tried using a DataSet but get the error "The same table
(Gid) cannot be the child table in two nested relations". The file
has a number of parent nodes at the "<ShipmentHeader>" level, each of
which have a number of child nodes. I will not know ahead of time
which of these parent/child node relationships will occur. I have
looked at the XmlTextReader, but it looks like I would have to
evaluate NodeTypes, ReadInnerXml, etc. Is there an easier way?

<?xml version="1.0" encoding="UTF-8" ?>
<Shipment>
<ShipmentHeader>
<ShipmentGid>
<Gid>
<DomainName>xxxxx</DomainName>
<Xid>xxxxxxx</Xid>
</Gid>
</ShipmentGid>
<ShipmentRefnum>
<ShipmentRefnumQualifierGid>
<Gid>
<Xid>xxxxx</Xid>
</Gid>
</ShipmentRefnumQualifierGid>
<ShipmentRefnumValue>xxxxx</ShipmentRefnumValue>
<ShipmentRefnum>xxxxx</ShipmentRefnum>
<ShipmentRefnumQualifierGid>
<Gid>
<Xid>xxxx</Xid>
</Gid>
</ShipmentRefnumQualifierGid>
<ShipmentRefnumValue>xxxxxx</ShipmentRefnumValue>
</ShipmentRefnum>
</ShipmentHeader>
</Shipment>
 
A

Artur Sorszegi

Hi!

You have too many levels in your XML file. To create an xml file that .NET
can read (without a schema), you should use the following structure.

<?xml ... ?>
<datasetname>
<tablename>
<columnname>columndata</columnname>
<columnname>columndata</columnname>
</tablename>
<tablename>
<columnname>columndata</columnname>
<columnname>columndata</columnname>
</tablename>
</datasetname>

So the document element becomes your DataSet. Below it, there are elements.
These will be sorted into tables. If these element names are the same, they
are added to the same table. Else, they are added to different tables.
Columnnames then are simply names of the columns, and they contain the data
you want in that column.

This is the case without a schema. With a schema, you could make it work
with other kinds of datasets, but by default, my experience is, it reads and
writes in this format.

-Artur

PS: some example

<customersdb>
<customer>
<name>Peter</name>
</customer>
<customer>
<name>Jane</name>
</customer>
<order>
<by>Peter</by>
<what>book</what>
</order>
<customersdb>

This would create a dataset named customersdb with two tables, named
customer and order. The customer table would have two rows. The order table
would have one row.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top