• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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
0
jkelly061597
Asked:
jkelly061597
  • 3
  • 2
1 Solution
 
Chester_M_RagelCommented:
Try like this, test will be the ones which you are looking for...

foreach(DataRow row in dtDataTable.Rows)
{
        string test = row[0].ToString();
}
0
 
jkelly061597Author Commented:
test now = "0"... Not the XML of elements One and Two...
0
 
Chester_M_RagelCommented:
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!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkelly061597Author Commented:
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...
0
 
jkelly061597Author Commented:
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.
0
 
ee_ai_constructCommented:
Question answered by asker or dialog valuable.
Closed, 200 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now