i have a browse button to load an Xml file and a textbox to show the path of
file and a list box to show the attributes of a node selected in the Tree
view.
My code is like this:
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);
}//constructor
private void treeDocLoadMethod(string nameofFile)
{
try
{
txtBoxfile.Text = nameofFile;
//Create XML document & load the XML file.
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;
AddNodes(treeViewObj.Nodes, xdoc.DocumentElement);
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
;
tnode.Nodes.Add(new TreeNode(xNode.Name));
treeNode = tnode.Nodes;
ConvertAllXmlnodetoTreenode(xNode,treeNode.Nodes);
}//for
}//if
else
{
tnode.Text = xnode.OuterXml.Trim();
}//else
}
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 treeViewObj_AfterSelect(object sender, TreeViewEventArgs
e)
{
//listBoxshow.Items.Clear();
listBoxeg.Items.Clear();
XmlNode xNode = e.Node.Tag as XmlNode;
XmlAttributeCollection xmlAttColl = xNode.Attributes;
String name = "<" + xNode.Name;
if (xNode != null)
{
foreach (XmlNode subNode in xNode.ChildNodes)
{
listBoxeg.Items.Add(subNode.Name);
}
}
}//treeViewObj_AfterSelect
private void AddNodes(TreeNodeCollection treeNodes, XmlNode xNode)
{
TreeNode subNode = treeNodes.Add(xNode.Name);
subNode.Tag = xNode;
foreach (XmlNode subElement in xNode.ChildNodes)
{
AddNodes(subNode.Nodes, subElement);
}
}
}//class
}//namespace
But i am unable to get the desired results.i want wen a user selects a node
in treeview its attributes and values should be displayed in listbox.
can u help me providing the right code to be written in AfterSelect()
Tanx again
Regards
RAch
Frank said:
Hi
Well, it depends on what you like to do ??
With the line:
((System.Windows.Forms.TreeView)sender).SelectedNode
you get the currectly selected node.
In AfterSelect you can take all the informations of this node
and for example fill with this a TextBox or a ListView ... or
do something else ...
What do you need to do ??
Regards
Frank Uray
[quoted text clipped - 19 lines]
Please help me out as early as possible..
Thanks in advance.