DataSet.ReadXml()

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

I've been toying with DataSet.ReadXml(). I'm able to call this method and
load an XML file. However, my data is hierarchical as follows:

<Categories>
<Category Name="XXX1">
<Subcategory>...</Subcategory>
<Subcategory>...</Subcategory>
<Subcategory>...</Subcategory>
<Subcategory>...</Subcategory>
</Category>
<Category Name="XXX2">
<Subcategory>...</Subcategory>
<Subcategory>...</Subcategory>
<Subcategory>...</Subcategory>
<Subcategory>...</Subcategory>
</Category>
</Categories>

When I examine the returned rows, I only have the categories and not the
subcategories.

I'll be reading more about this and will eventually understand it better.
But if anyone know a fairly simple way to make this work, that would be much
appreciated.

Thanks.

Jonathan
 
Hi, Jonathan

I think the ReadXml Method can't return the subcategories to you because it
don't know the structure of your data. It's just read the first level
elements , as you see
 
I wont believe this is a valid XML dataset as I see in one table 4 times the
row item subcategory (those should be unique inside the table)

Cor
 
Yeah, I was thinking is that I need to define the structure. However, the
DataSet has the ability to load simple data automatically. I was just
curious if there was some easy way to have it understand a second level of
data.
 
Hi, Jonathan

Probably we can try this task follow below
1. Use the Typed DataSet ahead of use the Normally DataSet
As you know , the Normally DataSet can not understand your data
structure,but the Typed DataSet can !
and how we can create the Typed DataSet? actually, the Typed DataSet just a
class which derive from System.DataSet.If you already have the data
structure, you can use the xsd.exe to do this with two step.
The first step: xsd yourdatafile.xml (Then, will generate a file named
yourdatafile.xsd, It's the schema of your data)
The second step: xsd yourdatafile.xsd /d (Then,will generate a file
named yourdatafile.cs)

You can add the yourdatafile.cs to your project as an exists item. Then, you
can use the class to read xml:
yourdatafile ds=new yourdatafile();
ds.ReadXml(xmlFileName);


regard the "yourdatafile" class have the full structure of your data.
actually, it use the property to describe all the element or attribute of
the data file.
Then, I think you can bind the data now.

2.Create a class derive from System.DataSet, then add a method to it, like
this
namespace ReadXmlSample
{
public class MyDataSet:DataSet
{
public void ReadXmlByPath(string xmlFileName,string xpath,bool
includeAttribute,string tableName) {

}
}
}
 
Back
Top