Hi everyone,
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.
}
What i have done till now is here :
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
This dosent show any errors but i am not getting the desired results..
Please help me out as early as possible by providing the right part of code to be written..
Thanks in advance.
Loading
Rach SinghPosted Feb 27, 2009, 3:02 PM
I have created a winform having a browse button(to upload a file from my hard disk) ,textbox(to show the file i selected),clear button,Exit button,Treeview(to display the Xml file),listbox(where i have to show a attributes and values of a selected node).Now i have to write a code in treeView_AfterSelect() where when a user clicks on any node in a treeview,the listbox displays its attributes and values.Now i want to use TAG,but i dont know how to start with it.Can you help out??
Thanks
Bechir BejaouiPosted Feb 23, 2009, 5:42 AM
Thanks
Rach SinghPosted Feb 21, 2009, 4:56 AM
Tanx for replyg. But do you hav any other option other then using XML Parser.i want to use TAG in AfterSelect() can u tell me the syntax.tanx....
Bechir BejaouiPosted Feb 18, 2009, 6:50 PM
add System.Xml.Linq and System.IO to your code
suppose this xml file template
And I want to parse this XML file
I save it let's say in C:\
private void ParseXml(string path, TreeView treeview)
{
StreamReader oReader = new StreamReader(path);
string Content = oReader.ReadToEnd();
XElement xe = XElement.Parse(Content);
int i = 0;
List
foreach (XElement el in xe.Elements())
{
list.Add(new TreeNode(el.Name.ToString() + " : " + el.Value));
foreach (XAttribute attr in el.Attributes())
{
list[i].Nodes.Add(new TreeNode(attr.Name.ToString() + " : " + attr.Value));
}
i++;
}
treeview.Nodes.AddRange(list.ToArray());
}
then I call it fromwithin my code
ParseXml(@"C:\Family.xml", treeView1);