Change treeview attributes values C# - c#

I am loading an XML file and showing it as a treeview. I would like to allow the user to see what children can each element have. Is there any way to do so?
I am having troubles matching the nodes (the tree node with the 'original' node). I compared them by name but I don't always get the correct result.
This it what I have so far:
xmlFile = new XmlDocument();
xmlFile.Load(dialog.FileName);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xmlFile.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];
AddNode(xmlFile.DocumentElement, tNode);
treeView1.ExpandAll();
Adding the nodes
public void AddNode(XmlNode xmlNode, TreeNode treeNodes)
{ XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
if (xmlNode.HasChildNodes)
{nodeList = xmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{xNode = xmlNode.ChildNodes[i];
treeNodes.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNodes.Nodes[i];
AddNode(xNode, tNode);}}
else
{ treeNodes.Text = (xmlNode.OuterXml).Trim();}}
On tree node click
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e){
List<string> temp = new List<string>();
this.lbElements.Items.Clear(););
foreach (XmlNode node in xmlFile.DocumentElement.ChildNodes)
{
if (node.Name == e.Node.Name)
{
foreach (string s in fh.getChildNodes(node)) temp.Add(s);
if (!temp.Contains(s))
temp.Add(s);
foreach (string s in temp) this.lbElements.Items.Add(s);

Try using if (node.Name == e.Node.Text && node.NodeType == XmlNodeType.Element)
in the treeview_NodeMouseClick().

The simpest way to do is to use XmlReader and loop through the content.
I tried in this way.
>
private void treeView1_NodeMouseClick_1(object sender,
> TreeNodeMouseClickEventArgs e)
> {
> XmlReader reader = null;
> reader = XmlReader.Create(filePath);
> {
> reader.MoveToContent();
> // Parse the file and display each of the nodes.
> while (reader.Read())
> {
> switch (reader.NodeType)
> {
> case XmlNodeType.Element:
> if (reader.LocalName == "to")
> {
>
> }
> break;
>
> case XmlNodeType.Text:
> if(reader.Value == "Jane")
> {
>
> }
> break;
> }
> }
> }
> }
Xml file used is:
<note>
<to>Tove</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Weekend</body>
</note>

Related

C# Populate treeView with files in a n-depth

I don't know how to show show all data in a treeview control:
here is my code:
private void PopulateTree(string path, int depth, TreeNode parent)
{
if (depth == 0)
{
//This make a child
parent.Nodes.Add(new TreeNode(path);
return;
}
//This makes a parent
TreeNode first = new TreeNode(path);
parent.Nodes.Add(first);
foreach (var v in ListWithPaths)
{
PopulateTree(v, depth - 1, first);
}
}
It only seems to works when depth=1
parent
-parent
--parent
---child
---child
--parent
---child
---child
-/parent
/parent
This is how I see it....
I have found this answer from here :
private void Form1_Load(object sender, EventArgs e)
{
var paths = new List<string>
{
#"C:\WINDOWS\AppPatch\MUI\040C",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
#"C:\WINDOWS\addins",
#"C:\WINDOWS\AppPatch",
#"C:\WINDOWS\AppPatch\MUI",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};
treeView1.PathSeparator = #"\";
PopulateTreeView(treeView1, paths, '\\');
}
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
}
}
foreach (var v in ListWithPaths)
{
PopulateTree(v, depth - 1, first);
}
}
Based on the path, it's creating the depth.
If this is not the wished behaviour, you can change the PopulateTreeView-Function accordingly.
There are some unclear moments in your code:
Do you want to explicitly restrict path depth (e.g. show paths up to 3d level only)
What is ListWithPaths? Can two paths have a common node?
To show a single path with restriction (depth) the code could be
private void PopulateTree(String path, TreeNode parent, int depth) {
if (depth == 0) // <- Artificial criterium
return;
if (String.IsNullOrEmpty(path))
return;
int index = path.IndexOf(Path.DirectorySeparatorChar);
String directoryName = (index < 0) ? path : path.Substring(0, index);
String otherName = (index < 0) ? null : path.Substring(index + 1);
TreeNode childNode = parent.Nodes.Add(directoryName);
PopulateTree(otherName, childNode, depth - 1);
}
In order to load a collection of paths without any restriction with possible common nodes you can use
something like this:
private void PopulateTree(String path, TreeView view, TreeNode parent) {
if (String.IsNullOrEmpty(path))
return;
int index = path.IndexOf(Path.DirectorySeparatorChar);
String directoryName = (index < 0) ? path : path.Substring(0, index);
String otherName = (index < 0) ? null : path.Substring(index + 1);
TreeNode childNode = null;
TreeNodeCollection nodes = (parent == null) ? view.Nodes : parent.Nodes;
foreach (TreeNode node in nodes)
if (String.Equals(node.Name, directoryName)) {
childNode = node;
break;
}
if (childNode == null)
childNode = nodes.Add(directoryName);
PopulateTree(otherName, view, childNode);
}
private void PopulateTree(IEnumerable<String> paths, TreeView view) {
view.BeginUpdate();
try {
foreach (String path in paths)
PopulateTree(path, view, null);
}
finally {
view.EndUpdate();
}
}
...
PopulateTree(ListWithPaths, MyTreeView)

How I can set a ImageIndex in my TreeView by a if statement?

hi how i can set a image for my treeView nodes... I have a parent and a child node.
here is my code:
private void btnShowLicstate_Click(object sender, EventArgs e)
{
treeLic.Nodes.Clear();
string command = "\"C:\\lmxendutil.exe\" -licstatxml -host lwserv005 -port 6200";
string output = ExecuteCommand(command);
string final_output = output.Substring(90, output.Length - 90);
XmlReader xr = XmlReader.Create(new StringReader(final_output));
var xDoc = XDocument.Load(xr);
TreeNode root = new TreeNode();
LoadTree(xDoc.Root.Element("LICENSE_PATH"), root);
treeLic.Nodes.Add(root);
treeLic.ImageList = imageList1;
}
public void LoadTree(XElement root, TreeNode rootNode)
{
foreach (var e in root.Elements().Where(e => e.Attribute("NAME") != null))
{
var node = new TreeNode(e.Attribute("NAME").Value);
rootNode.Nodes.Add(node);
if (e.Name == "FEATURE")
{
node.SelectedImageIndex = 1;
}
else if (e.Name == "USER")
{
node.SelectedImageIndex = 0;
}
LoadTree(e, node);
}
}
my problem is that i have everyone the same picture but i want for FEATURE the index 1 and for USER the Index 2 but why it don't work ? :(
You should use ImageIndex property instead of SelectedImageIndex.
The first one is the index from ImageList for node in unselected state and the second one is applied when you select node using mouse, keyboard or through code.

.NET- Treeview Control

<?xml version="1.0" encoding="UTF-8" ?>
<properties>
<general>
<title type="textbox">title1</title>
<subtitle type="textbox">subtitle1</subtitle>
<radius type="textbox">20</radius>
</general>
<behavior>
<sorting>
<enable type="checkbox">True</enable>
<by type="dropdown">Data</by>
<order type="dropdown">Descending</order>
</sorting>
</behavior>
<appearance>
<series>
<innerseries type="colorpicker">#996666</innerseries>
<innerseries type="colorpicker"></innerseries>
<innerseries type="colorpicker"></innerseries>
<transparency type="slider">19</transparency>
</series>
</appearance>
</properties>
Above is my XML.
In My .ASPX Page:
<asp:TreeView ID="TreeView1" runat="server"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged1" ImageSet="Arrows"
EnableTheming="true">
</asp:TreeView>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
In my Codebehind I dynamically load an xml and build a treeview
protected void Button1_Click(object sender, EventArgs e)
{
doc.LoadXml("XmlFile");
XmlNode node = doc.DocumentElement;
TreeView1.Nodes.Clear();
TreeView1.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = TreeView1.Nodes[0];
Property obj = new Property();
obj.AddChildNode(node, tNode);
}
In my Property.cs (Class File)
public void AddChildNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
//if (inXmlNode.HasChildNodes)
if (inXmlNode.ChildNodes.Count >= 1 && inXmlNode.ChildNodes[0].HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddChildNode(xNode, tNode);
}
}
else
{
if (inXmlNode.ChildNodes.Count > 1)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddChildNode(xNode, tNode);
}
}
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
else
{
if (inXmlNode.ChildNodes.Count == 0)
{
inTreeNode.Text = (inXmlNode.Name).Trim();
inTreeNode.Target = inXmlNode.Attributes[0].Value;
}
else
{
inTreeNode.Text = (inXmlNode.Name).Trim();
inTreeNode.Target = inXmlNode.Attributes[0].Value;
inTreeNode.Value = inXmlNode.ChildNodes[0].Value;
}
}
//inTreeNode.Value = (inXmlNode.OuterXml).Trim();
}
}
The Problem here is, when i run my code and select the Title or Subtile node,the focus is set to that node and SelectIndexChanged event is fired.But, when i select the First Child (Innerseries )it wirks perfectly.When second Innerseries Child is selected,the selection is focused on that node.But, when i Select the Third Child(Innereseries), the focus is on the Second Child .So, it looks like when the node name and value are same ,it selects the first node .

Treeview implementation child nodes missing

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i = 0;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= (nodeList.Count - 1); i++)
{
xNode = inXmlNode.ChildNodes[i];
if (null != xNode)
{
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Attributes[0].Value));
tNode = inTreeNode.ChildNodes[i];
AddNode(xNode, tNode);
}
}
}
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
}
}
But am getting only parent node and child nodes are not added . After going through various sites i learned that this is the error
inTreeNode.Nodes.Add(new TreeNode(xNode.Attributes[0].Value));
tNode = inTreeNode.Nodes[i];
but am not getting inTreeNode.Nodes option.
Thanks for Help
Nodes collection is at treeview level and try to look the implementation here

Recursively adding a ChildNode to a Parent Node

I have a problem that I cannot seem to solve.
I am building a TreeView dynamically and I have an ordered list. I want the TreeView to build in such a way:
Node1
_Node2
__ Node3
__ _Node..N
My code is as follows:
TreeNode tn = new TreeNode();
for (int i = 0; i < EmployeesReportingLine.Count; i++ )
{
Employee ep = EmployeesReportingLine[i];
while (tn.ChildNodes.Count > 0)
tn = tn.ChildNodes[0];
TreeNode temp = new TreeNode(ep.FullName);
if (i > 0)
tn.ChildNodes.Add(temp);
else
tn = temp;
}
TreeView1.Nodes.Add(tn);
I have made several other attempts at using recursive functions but the snippet above was my best attempt.
Thanks in advance.
private void addNode(TreeNodeCollection nodes, TreeNode newnode) {
if (nodes.Count == 0) nodes.Add(newnode);
else addNode(nodes[0].Nodes, newnode);
}
Or:
private void addNode2(TreeNode start, TreeNode newnode) {
if (start.Nodes.Count == 0) start.Nodes.Add(newnode);
else addNode2(start.Nodes[0], newnode);
}

Categories

Resources