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.. what i have done till now is..
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 = "<!--" + xmlNode.Value + "-->";
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 listBoxshow_SelectedIndexChanged(object sender, EventArgs e)
{
}
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
But this dosent work Please correct me and give provide me with right part of code required to run this project, if you can help out.... Thanks in advance.....[:)]