Hi,
I have to create a project in C# in which i have to create a XML Tree View, select a xml file from hard drive(System) it is displayed as Tree View on winform.

When a user selects a particular node/element in the tree view,its attributes and values have to be displayed in list box and text box respectively.

Till now i have done a part of it like selecting it from Openfiledialog(Browse) showing it in tree View and all the attributes related to the XML file in list box.
But i don't know how to put a code in
private void treeViewObj_AfterSelect(object sender, TreeViewEventArgs e)
{
//what to write here.
}
Please help me out as early as possible..
Thanks in advance.:)

Dani AI

Generated

As noted, the selected TreeNode is available from the event args, but the real issue in 's snippet is that you always call xdoc.DocumentElement inside AfterSelect — that gives you the document root, not the element the user clicked. The simplest, most robust approach is:

  1. When you build the TreeView from the XmlDocument, store the corresponding XmlNode in each TreeNode's Tag.
  2. In AfterSelect cast e.Node.Tag back to XmlNode, clear the ListBox, then enumerate that node's Attributes.
  3. Add the actual XmlAttribute objects to the ListBox (WinForms ListBox can hold objects) and set DisplayMember = "Name".
  4. In the ListBox SelectedIndexChanged handler cast the selected item to XmlAttribute and put attr.Value into the TextBox. Also consider showing the element text with xmlNode.InnerText if you want element content rather than an attribute value.

Example pattern (concise):

// when building the tree:
var tn = new TreeNode(xmlElem.Name) { Tag = xmlElem };
// add children...

// AfterSelect:
var xmlNode = e.Node.Tag as XmlNode;
if (xmlNode == null) return;
listBoxAttributes.Items.Clear();
if (xmlNode.Attributes != null)
{
    listBoxAttributes.DisplayMember = "Name";
    foreach (XmlAttribute a in xmlNode.Attributes) listBoxAttributes.Items.Add(a);
}
textBoxValue.Text = xmlNode.InnerText ?? "";

// listbox selection:
var sel = listBoxAttributes.SelectedItem as XmlAttribute;
textBoxValue.Text = sel != null ? sel.Value : xmlNode.InnerText;

Troubleshooting: verify Tag is set when you create each TreeNode; check xmlNode.Attributes for null; cast carefully to XmlAttribute before accessing Value. This pattern keeps tree UI and XML model in sync and avoids repeatedly searching the XmlDocument for the selected node.

Recommended Answers

All 2 Replies

Hello, rasingh24.
I suppose, this should be helpful for you: TreeViewEventArgs..::.Node Property

P.S. All you have to do to get any data of the selected node - is extract it from e.Node .

Thanks for the reply......
I have already gone through the link you provided but since i am new to C# i am not able to get how these work..
i mean what to put in the AfterSelect() method..i have written some code mailing you up...please correct me if i am wrong....

private void treeViewObj_AfterSelect(object sender, TreeViewEventArgs e)
{
listBoxeg.Items.Clear();//Listbox where i have to show the attributes of the node selected in the treeview.
XmlNode xNode = xdoc.DocumentElement;
TreeNode selNode = e.Node;
textBox1.Text = selNode.Text;//Textbox where i want to show the value of the attribute.


if (e.Node.TreeView.SelectedNode != null)
{
foreach (XmlAttribute attribute in xNode.Attributes)
{
ShowinListboxandTextbox(attribute, selNode.Nodes);
}
}
}


private void ShowinListboxandTextbox(XmlNode xmlNode,TreeNodeCollection treeNodecollect)
{
TreeNode treeNodeobj = treeNodecollect.Add(xmlNode.Name);


switch (xmlNode.NodeType)
{
case XmlNodeType.Attribute:
treeNodeobj.Text = "ATTRIBUTE: " + xmlNode.Name;
listBoxeg.Items.Add(treeNodeobj.Text);
break;
case XmlNodeType.CDATA:
treeNodeobj.Text = xmlNode.Value;
listBoxeg.Items.Add(treeNodeobj.Text);
break;
}//switch
}//ShowinListboxandTextbox

But this dosent work :( Please correct me if you can help out....:S
Thanks...:sad:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.