Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read XML Attributes

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hi
I have an xml doc set up like this
Code:
<root>
 <category name='name'>
  <element att1='value' att2='value' att3='value' />
 </category>
 <category name='name2'>
  <element att1='value' att2='value' att3='value' />
 </category>
</root>
I was successfull in reading the file and create a treeview from the xml doc. Now what I need to do is, when the user selects one of the treeview options (value of att1), I will need to reread the xml file to get the value of another attribute (att2) to tell me what to show. I am hoping that i do not need to reread the file line by line again. Is there a way around that. XPath does not seem to help me due to the way I have the xml doc set up. So I need to know how I need to do this. The alternate to all this would be to put a associate a value with the treeview display, but I not sure if that can be done, can it? (sort of like the DisplayMember and ValueMember of a comboBox)

Thanks,
RT
 
As long as you have good XML XPath should work. The syntax is sometimes tricky.

You can use the TreeNode.Tag property to hold a node of XML until you need it.



For example
XmlNode node = (XmlNode)uxTreeView.SelectedNode.Tag
//Then you can an attribute like this
node.Attributes["att1"].Value
 
I am confused by your example:
XmlNode node = (XmlNode)uxTreeView.SelectedNode.Tag

Can you please elaborate.

Here is more info. When the user clicks on the TreeView option, i take the full path and pass it to a member in a different class. I take the passed string and split it up by the \ character. Do I need to call the XPath class?

Thanks!
 
Here is an example...

Start a new windows application put a treeview and 3 textboxes on the form.

private void Form1_Load(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<root>
<category name='name1'>
<element att1='name1att1Value' att2='name1att2Value' att3='name1att3Value' />
</category>
<category name='name2'>
<element att1='name2att1Value' att2='name2att2Value' att3='name2att3Value' />
</category>
</root>");

TreeNode tNode;
foreach (XmlNode xNode in doc.SelectNodes("root/category"))
{//Fill the tree
tNode = new TreeNode(xNode.Attributes["name"].Value);
//Save the node you need for later use
tNode.Tag = xNode.SelectSingleNode("element");
treeView1.Nodes.Add(tNode);
}

}

private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
/Get the node you saved in the loading of the tree and use it for something
XmlNode node = (XmlNode)treeView1.SelectedNode.Tag;
textBox1.Text = node.Attributes["att1"].Value;
textBox2.Text = node.Attributes["att2"].Value;
textBox3.Text = node.Attributes["att3"].Value;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top