Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How 2show Selected treeview node

rasinghrasingh Posts: 3Member
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 [b]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[/b].
Till now i have done a part of it like selecting a XML file 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 [code]private void treeViewObj_AfterSelect(object sender, TreeViewEventArgs e)
{
//what to write here.
}[/code]
I have written the following with no errors but it is not showing the desired result
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Text;
using System.Windows.Forms;

namespace LoadXMLtreeDisplay
{
public partial class TreeDisplay : Form
{
//Declaring Treeview in Listbox
TreeView listViewobj = new TreeView();

//Declare XML Document object
XmlDocument xdoc = new XmlDocument();

//Constructor of class
public TreeDisplay( )
{
InitializeComponent();
this.Controls.Add(treeViewObj);
this.listBoxshow.SelectedIndexChanged += new System.EventHandler(this.listBoxshow_SelectedIndexChanged);
}//constructor

private void treeDocLoadMethod(string nameofFile)
{
try
{
//TextBox txtBoxfile = new TextBox();
txtBoxfile.Text = nameofFile;

//Create XML document & load the XML file.
//XmlDocument xdoc = new XmlDocument();
xdoc.Load(nameofFile);

this.treeViewObj.Nodes.Clear();
this.treeViewObj.Nodes.Add(new TreeNode(xdoc.DocumentElement.Name));
TreeNode tNodeObj = new TreeNode();
tNodeObj = this.treeViewObj.Nodes[0];

XmlNodeList nodeList = xdoc.SelectNodes("//settings/tables/table/name[. ='{0}']");
XmlNode xNode = xdoc.DocumentElement;//nodeList.Item(0).ParentNode;

//Declaring function for adding Nodes to tree View.
//AddingNodesToTree(xNode,tNodeObj);

ConvertAllXmlnodetoTreenode(xdoc, treeViewObj.Nodes);

treeViewObj.Nodes[0].Expand();
treeViewObj.CollapseAll();

Cursor = System.Windows.Forms.Cursors.Default;
}

catch (XmlException xmlex)
{
MessageBox.Show(xmlex.Message, "ERROR");
}//catch
catch(Exception exp)
{
txtBoxfile.Text = exp.Message;
}
}//treeDocLoadMethod

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

switch (xmlNode.NodeType)
{
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.XmlDeclaration:
treeNodeobj.Text = "<?" + xmlNode.Name + " " + xmlNode.Value + "?>";
break;
case XmlNodeType.Element:
treeNodeobj.Text = "<" + xmlNode.Name + ">";
break;
case XmlNodeType.Attribute:
treeNodeobj.Text = "ATTRIBUTE: " + xmlNode.Name;
listBoxshow.Items.Add(treeNodeobj.Text);
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
treeNodeobj.Text = xmlNode.Value;
listBoxshow.Items.Add(treeNodeobj.Text);
break;
case XmlNodeType.Comment:
treeNodeobj.Text = "";
break;
}//switch
if (xmlNode.Attributes != null)
{
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
ConvertAllXmlnodetoTreenode(attribute, treeNodeobj.Nodes);

}
}

foreach (XmlNode childNode in xmlNode.ChildNodes)
{
ConvertAllXmlnodetoTreenode(childNode, treeNodeobj.Nodes);

}

}// ConvertAllXmlnodetoTreenode Method

private void ConvertAddTreeNodestoTree(XmlNode xnode, TreeNode tnode)
{
XmlNode xNode;
TreeNode treeNode;
XmlNodeList nodeList;

if (xnode.HasChildNodes)
{
nodeList = xnode.ChildNodes;
for (int i = 0; i <= nodeList.Count-1;i++)
{
xNode = xnode.ChildNodes[i];
tnode.Nodes.Add(new TreeNode(xNode.Name));
treeNode = tnode.Nodes[i];
ConvertAllXmlnodetoTreenode(xNode,treeNode.Nodes);
}//for
}//if
else
{
tnode.Text = xnode.OuterXml.Trim();
}//else
}

private void ExpandBtn_Click(object sender, EventArgs e)
{
try
{
if (this.ExpandBtn.Text == "Expand TreeNodes")
{
this.treeViewObj.ExpandAll();
this.ExpandBtn.Text = "Collapse TreeNodes";
}//if
else
{
this.treeViewObj.CollapseAll();
this.ExpandBtn.Text = "Expand TreeNodes";
}//else
}//try
catch(Exception exp)
{
MessageBox.Show(exp.Message,"Error");
}//catch
}//Expand button

private void btnBrowse_Click(object sender, EventArgs e)
{
txtBoxfile.Clear();
listBoxshow.Items.Clear();
listBoxeg.Items.Clear();
ExpandBtn.Text = "Expand TreeNodes";
OpenFileDialog open = new OpenFileDialog();
//open.InitialDirectory = @"C:";
open.Filter = "XML Files(*.xml)|(*.xhtml)|All files(*.*)|*.*";
open.FilterIndex = 2;
open.RestoreDirectory = true;

if (open.ShowDialog(this)== DialogResult.OK)
{
txtBoxfile.Text = open.FileName;
treeDocLoadMethod(open.FileName); //this variable gives the name of selected file
}

}//Browse button

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{

}
private void btnClear_Click(object sender, EventArgs e)
{
listBoxshow.Items.Clear();
listBoxeg.Items.Clear();
txtBoxfile.Clear();
textBox1.Clear();

}//Clear button
private void treeViewObj_AfterSelect(object sender, TreeViewEventArgs e)
{
//listBoxshow.Items.Clear();
listBoxeg.Items.Clear();
XmlNode xNode = xdoc.DocumentElement;
TreeNode selNode = e.Node;
//textBox1.Text = selNode.Text;

//if(xNode.Attributes != null)
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

}//class
}//namespace[/code]
Please help out in giving me the correct lines of code so i can obtain the desired result?
I m new to C# so by googling i have written the following code..
Thanks in Advance... :-)


Sign In or Register to comment.