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
Related
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>
<?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 .
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);
}
I have many nodes in the treeview like nodes it children, children of children....
i wanted to copy those entire content and paste it in another node.
I dont want to use clone method since it affects object of original node from which it s copied.
Try using recursive like this
private void IterateTreeNodes( TreeNode originalNode, TreeNode rootNode )
{
foreach ( TreeNode childNode in originalNode.Nodes )
{
TreeNode newNode = new TreeNode( childNode.Text );
newNode.Tag = childNode.Tag;
treeView2.SelectedNode = rootNode;
treeView2.SelectedNode.Nodes.Add( newNode );
IterateTreeNodes( childNode, newNode );
}
}
// copy nodes from treeView1 to treeView2
private void button1_Click( object sender, EventArgs e )
{
foreach ( TreeNode originalNode in treeView1.Nodes )
{
TreeNode newNode = new TreeNode( originalNode.Text );
newNode.Tag = originalNode.Tag;
treeView2.Nodes.Add( newNode );
IterateTreeNodes( originalNode, newNode );
}
}
it's a code snippet from http://windowsclient.net/blogs/faqs/archive/2006/05/30/how-do-i-clone-or-copy-all-the-nodes-from-one-treeview-control-to-another.aspx
You can use any of the traversal algorithm
at each step, you can you can put the traversed node to an object and put that object in an objectList...
All the best...
I have a Treeview where on selecting a node the attributes and values has to be displayed in listbox.
In treeView1_AfterSelect, the text parsing code depends on the textual representation for a node in the tree view, which can be changed at any time and break the entire logic of list display. This strong dependency between the tree view and the list display should be eliminated by binding nodes in the treeview to nodes of the XML document, so that the raw Xml data can be used to display text in the list.What should i write here?
private static void AddingNodesToTree(XmlNode xmlNode,TreeNode tnode)
{
//Adding nodes to tree while looping through the entire XML file
if (xmlNode.HasChildNodes)
{
XmlNodeList nodeList = xmlNode.ChildNodes;
for (int i = 0; i <= nodeList.Count - 1; i++)
{
XmlNode xmladdtreeNode = xmlNode.ChildNodes[i];
String nodetype = "" + xmladdtreeNode.NodeType;
if (nodetype.Equals("Text") || nodetype.Equals("Comment"))
{
tnode.Nodes.Add(new TreeNode(xmladdtreeNode.InnerText));
}
else
{
String name = "<" + xmladdtreeNode.Name;
XmlAttributeCollection attCol = xmladdtreeNode.Attributes;
foreach (XmlAttribute xmlatt in attCol)
{
name += " " + xmlatt.Name + "=\"" + xmlatt.Value + "\"";
}
name += ">";
TreeNode tn = new TreeNode(xmladdtreeNode.Name);
tn.Text = name;
tnode.Nodes.Add(tn);
TreeNode treeNode = tnode.Nodes[i];
AddingNodesToTree(xmladdtreeNode,treeNode);
}
}//for
}//if
else
{
tnode.Text = xmlNode.OuterXml.Trim();
}//else
}//AddingNodesToTree
//To show Attributes and values of selected Nodes.
private void treeView1_AfterSelect(object sender,TreeViewEventArgs e)
{
TreeNode treenode = e.Node; //Node selected in Treeview
String text = treenode.Text;
String relevent = text;
Boolean flag = true;
while (flag)
{
int SpaceIndex = relevent.IndexOf(" ");
if (SpaceIndex != -1)
{
int indexofEqual = relevent.IndexOf('=', SpaceIndex);
if (indexofEqual != -1)
{
int indexOFValue = relevent.IndexOf("\"", indexofEqual + 2);
if (indexOFValue != -1)
{
String attribute = relevent.Substring(SpaceIndex + 1, indexofEqual - SpaceIndex - 1);
String value = relevent.Substring(indexofEqual + 2, indexOFValue - indexofEqual - 2);
listBox1.Items.Add("Attribute : " + attribute + " Value : " + value);
relevent = relevent.Substring(indexOFValue);
}
else
{
listBox1.Items.Add("Bad format of the xml file for this node");
flag = false;
}
}
else
{
flag = false;
}
}
else
{
flag = false;
}
}
}//AfterSelect()
Thanks....
If I understand the question correctly you want to get back to the XmlNode when a TreeNode is selected. The usual solution is to store the XmlNode in the Tag property:
TreeNode tn = new TreeNode();
tn.Text = name;
tn.Tag = xmladdtreeNode;
and in the AfterSelect
TreeNode treenode = e.Node;
XmlNode xmlNode = (XmlNode) treeNode.Tag;
i tried this but got the NullRefrenceException at this line
foreach (XmlAttribute xmlatt in attCol) on attcol
This is the code i have written..
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
listBox1.Items.Clear();
XmlNode xNode = e.Node.Tag as XmlNode;
XmlAttributeCollection attCol = xNode.Attributes;
foreach (XmlAttribute xmlatt in attCol)
{
listBox1.Items.Add(xmlatt.Name);
listBox1.Items.Add(xmlatt.Value);
}
} //AfterSelect()