Populating TreeView from XML file

I

IndeX?

Hello,
I'm parsing from an xml file (using DOM), so that i can place certain nodes
in a TreeView.
Xml file looks like this:
<questions>
<question ID="Question 01" Text="Some Text">
<answers>
<answer OID="001">Some Text</answer>
<answer OID="002">Some Text</answer>
<answer OID="003">Some Text</answer>
<answer OID="004">Some Text</answer>
<answer OID="005">Some Text</answer>
</answers>
</question>
<question ID="Question 02" Text="Some Text">
<answers>
<answer OID="001">Some Text</answer>
<answer OID="002">Some Text</answer>
<answer OID="003">Some Text</answer>
<answer OID="004">Some Text</answer>
<answer OID="005">Some Text</answer>
</answers>
</question>
</questions>

treeview should look like this

-questions
-Question 01
-Question 02

Please help me out :(
Any help is appreciated !!!
 
M

Marc Scheuner [MVP ADSI]

Do something like this (assuming your file is called "sample.xml" and
located in the same directory as your executable):

private void LoadXmlAndFillTree()
{
// create "XmlDocument" instance
XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.Load("sample.xml");

// read top-level node and add as root node to tree
TreeNode oRootNode =

treeView1.Nodes.Add(oXmlDoc.DocumentElement.LocalName);

// add all the questions to the root node
XmlNodeList oListOfQuestions =
oXmlDoc.SelectNodes("questions/question");

foreach(XmlNode oNode in oListOfQuestions)
{
oRootNode.Nodes.Add(oNode.Attributes["ID"].Value);
}

oRootNode.Expand();
}

That should do the trick.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
C

Chris Murphy via DotNetMonster.com

If that were the case then we wouldn't need this site.
 

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