Dataset with XML

M

Maziar Aflatoun

Hi everyone,

I like to use the dataset object for reading the following XML.

<?xml version="1.0" encoding="utf-8"?>
<ORDERS>
<ORDER>
<GENERALCONTACT>
<FirstName>FirstName1</FirstName>
<LastName>LastName1</LastName>
<Company>Company1</Company>
</GENERALCONTACT>
<PRODUCTS>
<PRODUCT>
<NAME>Product A</NAME>
<PRICE>$10.99</PRICE>
</PRODUCT>
<PRODUCT>
<NAME>Product B</NAME>
<PRICE>$5.99</PRICE>
</PRODUCT>
</PRODUCTS>
</ORDER>

<ORDER>
<GENERALCONTACT>
<FirstName>FirstName2</FirstName>
<LastName>LastName2</LastName>
<Company>Company2</Company>
</GENERALCONTACT>
<PRODUCTS>
<PRODUCT>
<NAME>Product C</NAME>
<PRICE>$21.99</PRICE>
</PRODUCT>
<PRODUCT>
<NAME>Product D</NAME>
<PRICE>$6.99</PRICE>
</PRODUCT>
</PRODUCTS>
</ORDER>
</ORDERS>

Question 1. How do I just read the products for 1 order only? if I do
dsXMLOrder.Tables["PRODUCT"].Rows.Count;

it will return 4.

Question 2. If I was to add attributes to the above XML. Is it possible to
read attributes from a dataset loaded from an XML file?

Thank you
Maz A.
 
N

Nathan Neitzke

Mr. Aflatoun,
There are two ways I can think of doing this. The first is to load all the
XML data into an XMLDocument class and perform an XPath expression on the
XMLDocument (doc.SelectNodes("")) to get back the XML Fragment containing
just what you want.

The other way would be to use an XMLTextReader to read in the XML, and
filter it as you read it in.

Then when you have your XML document (filtered) you can easily convert it to
a DataSet using the XMLDataDocument class.

If you need more information, let me know. But this should be something to
go off of.

Take care.
 
D

Derek Harmon

Maziar Aflatoun said:
Question 1. How do I just read the products for 1 order only? if I do
dsXMLOrder.Tables["PRODUCT"].Rows.Count;

It returns 4 because you have a GENERALCONTACT table, a
PRODUCT table, and then you have a PRODUCTS table that
exists only to relate multiple PRODUCT rows to each other, and
an ORDERS table that exists only to relate multiple ORDERS
to each other. Every nested XML element will produce one of
these DataTables that exists only to relate other tables to each
other.

Your ORDERS table will contain a hidden column named Order_Id
or something like that. When you read all of the PRODUCT rows
that have that Order_Id, then you'll have the products by order.

Perhaps use the default view off the DataSet and set a RowFilter
expression on it.
Question 2. If I was to add attributes to the above XML. Is it
possible to read attributes from a dataset loaded from an XML
file?

Usually yes, if the attributes are in the same namespace URI (or
unqualified) then they will create DataColumns which have an
ColumnMapping of MappingType.Attribute. The DataColumns
are probably be typed as strings unless the document is associated
with an XML schema that says otherwise.

From the DataSet's perspective, these attributes look like any other
DataColumn. You can tell they came from an attribute by examining
their ColumnMapping property.


Derek Harmon
 
V

VJ

Did u try dsXMLOrder.Tables["Product"].Select(strSelect);

strSelect is any standard select statment where clause (u don't need to
specify the keyword where), and the above statment will return DataRow[]...
you can then get the first row..

VJ
 

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