jkelly061597
asked on
Getting to Desired XML data in DataSet
I have the following XML:
<root>
<level name="hello">
<term>One</term>
<term>two</term>
</level>
<level name='goodbye>
<term>Three</term>
<term>Four</term>
</level>
</root>
This is loaded into a DataSet... now how to I access only One and Two, then only Three and Four?
dsDataSet.LoadXml(xml);
dtDataTable = dsDataSet.Tables[1];
... here is where I get stuck.
I can get to the entire list of term elements using Tables[2], but I just want the subsets. Once I have them I want to foreach through them.
Thanks
<root>
<level name="hello">
<term>One</term>
<term>two</term>
</level>
<level name='goodbye>
<term>Three</term>
<term>Four</term>
</level>
</root>
This is loaded into a DataSet... now how to I access only One and Two, then only Three and Four?
dsDataSet.LoadXml(xml);
dtDataTable = dsDataSet.Tables[1];
... here is where I get stuck.
I can get to the entire list of term elements using Tables[2], but I just want the subsets. Once I have them I want to foreach through them.
Thanks
ASKER
test now = "0"... Not the XML of elements One and Two...
It is strange, I did a test with this,
<root>
<level name="hello">
<term>One</term>
<term>two</term>
</level>
<level name="goodbye">
<term>Three</term>
<term>Four</term>
</level>
</root>
and
DataSet ds = new DataSet();
ds.ReadXml(@"c:\a.xml");
DataTable dtDataTable = ds.Tables[1];
string test = "";
foreach(DataRow row in dtDataTable.Rows)
{
test = row[0].ToString();
}
and the result was one,two,three and four!
<root>
<level name="hello">
<term>One</term>
<term>two</term>
</level>
<level name="goodbye">
<term>Three</term>
<term>Four</term>
</level>
</root>
and
DataSet ds = new DataSet();
ds.ReadXml(@"c:\a.xml");
DataTable dtDataTable = ds.Tables[1];
string test = "";
foreach(DataRow row in dtDataTable.Rows)
{
test = row[0].ToString();
}
and the result was one,two,three and four!
ASKER
Okay, I concurr. However what I want two sets of data One, Two and Three, Four.
Since One and Two are under "Hello" and Three and Four are under "Goodbye" I want to process them differently...
Since One and Two are under "Hello" and Three and Four are under "Goodbye" I want to process them differently...
ASKER
I solved the problem by placing this XML into an XmlDocument (System.Xml) and using an xPath query for the desired node & children.
Example xPath to retrieve the first level node:
/root/level[@name='hello']
From here you have an XmlNode and you can select the child nodes to get at each term.
Example xPath to retrieve the first level node:
/root/level[@name='hello']
From here you have an XmlNode and you can select the child nodes to get at each term.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
foreach(DataRow row in dtDataTable.Rows)
{
string test = row[0].ToString();
}