Link to home
Start Free TrialLog in
Avatar of jkelly061597
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
Avatar of Chester_M_Ragel
Chester_M_Ragel

Try like this, test will be the ones which you are looking for...

foreach(DataRow row in dtDataTable.Rows)
{
        string test = row[0].ToString();
}
Avatar of jkelly061597

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!
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...
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.
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial