Click here to Skip to main content
15,889,366 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hello,

I have a treeview. It is similar to this;

Root
-Child 1
-Child a of Child 1
-Child b of Child 1

I am adding the child a and child b from textboxes but it just keeps adding the values under of the child 1. Instead I want to add them on a new node. How can I do this?

Like;

VB
Root
    -Child 1
       -Child a of Child 1
       -Child b of Child 1
    -Child 2
       -Child a of Child 2
       -Child b of Child 2
    -Child 3
       -Child a of Child 3
       -Child b of Child 3
Posted
Comments
fjdiewornncalwe 22-Nov-10 16:31pm    
Try showing us how you try do this in "code", not your sample assignment output. Then we may be able to help you where you go wrong. My guess is that you haven't even tried to use this really cool tool called "Google" to check this out.
Orcun Iyigun 24-Nov-10 15:51pm    
Instead of using a treeview I used a listview. So if you really want a proof of what I did here it is:
<pre lang='vb'>XmlDocument xmlDocument = new XmlDocument();
XmlElement credentialsRoot = xmlDocument.CreateElement("Credentials");
XmlElement credentialNode = xmlDocument.CreateElement("Credential");
XmlElement accesskeyNode = xmlDocument.CreateElement("AcccessKey");
XmlElement secretkeyNode = xmlDocument.CreateElement("SecretKey");
XmlElement selleridNode = xmlDocument.CreateElement("SellerID");

XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDocument.AppendChild(xmlDeclaration);

credentialNode.AppendChild(credentialsRoot);

xmlDocument.AppendChild(credentialsRoot);

accesskeyNode.AppendChild(credentialNode);
xmlDocument.AppendChild(credentialsRoot);

secretkeyNode.AppendChild(credentialNode);
xmlDocument.AppendChild(credentialsRoot);

selleridNode.AppendChild(credentialNode);
xmlDocument.AppendChild(credentialsRoot);

xmlDocument.Load(@"C:\Users\Orcun Iyigun\Desktop\Projects\XML\AmazonCredentials.xml");

XmlNodeList credentials = xmlDocument.DocumentElement.SelectNodes("/Credentials/Credential");
foreach (XmlNode xn in credentials)
{
string accesskeyCre = xn["AccessKey"].InnerText;
string secretkeyCre = xn["SecretKey"].InnerText;
string selleridCre = xn["SellerID"].InnerText;
}

XmlNodeList accesskeyCreNode = xmlDocument.GetElementsByTagName("AccessKey");
XmlNodeList secretkeyCreNode = xmlDocument.GetElementsByTagName("SecretKey");
XmlNodeList selleridCreNode = xmlDocument.GetElementsByTagName("SellerID");

string CredentialDetails = accesskeyCreNode + " " + secretkeyCreNode + " " + selleridCreNode;
XmlNode credentialsnodedetails = xmlDocument.SelectNodes("/Credentials/Credential")[0];
XmlNode newnode = credentialsnodedetails.CloneNode(true);


for (int i = 0; i < this.lvFeedCredentials.Items.Count; i++)
{
// Add the element and its attribute to the document
newnode.SelectSingleNode("AccessKey").InnerText = lvFeedCredentials.Items[i].SubItems[0].Text;
newnode.SelectSingleNode("SecretKey").InnerText = lvFeedCredentials.Items[i].SubItems[1].Text;
newnode.SelectSingleNode("SellerID").InnerText = lvFeedCredentials.Items[i].SubItems[2].Text;
}

// append the new node to the document
xmlDocument.DocumentElement.AppendChild(newnode);

// update the gridview to refresh display
XmlNodeList getcredentials = xmlDocument.GetElementsByTagName("Credential");

try
{
xmlDocument.Save(@"C:\Users\Orcun Iyigun\Desktop\Projects\XML\AmazonCredentials.xml");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
</pre>
So this loads the xml file and writes to its nodes from the listview columns.

and this is for the adding subitems to listview:
ListViewItem credentialItem = new ListViewItem(txtFeedCreAccessKey.Text);
credentialItem.SubItems.Add(txtFeedCreSecretKey.Text);
credentialItem.SubItems.Add(txtFeedCreSellerID.Text);
lv.Items.Add(credentialItem);

Hope you satisfied with it? And apologies for not using google enough to look for an answer and filling the forum with that question.
Matthew Hazlett 24-Nov-10 17:45pm    
You might want to revisit this. The entire top part of this code is not even used. You create an xml element just to blow it away a few lines later.

Then this line:
XmlNodeList credentials = xmlDocument.DocumentElement.SelectNodes("/Credentials/Credential");

Should be:
XmlNodeList credentials = xmlDocument.SelectNodes("//Credentials/Credential");

A single slash in an xPath means to return only one result. If there is only one node that ever gets returned then the foreach line under it makes no sense.

There are so many more issues with what you posted. It looks like you stole code from various sources, cut and pasted it together without really understanding anything at all about what you are doing.

May i add another example some KISS (Keep It Simple & Stupid):

If you already have a treeView or just created an instance of it:
Let's populate with some data - Ex. One parent two child's :
C#
treeView1.Nodes.Add("ParentKey","Parent Text");
treeView1.Nodes["ParentKey"].Nodes.Add("Child-1 Text");
treeView1.Nodes["ParentKey"].Nodes.Add("Child-2 Text");


Another Ex. two parent's first have two child's second one child:

C#
treeView1.Nodes.Add("ParentKey1","Parent-1  Text");
treeView1.Nodes.Add("ParentKey2","Parent-2 Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("Child-1 Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("Child-2 Text");
treeView1.Nodes["ParentKey2"].Nodes.Add("Child-3 Text");

Take if farther - sub child of child 2:
C#
treeView1.Nodes.Add("ParentKey1","Parent-1  Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("Child-1 Text");
treeView1.Nodes["ParentKey1"].Nodes.Add("ChildKey2","Child-2 Text");
treeView1.Nodes["ParentKey1"].Nodes["ChildKey2"].Nodes.Add("Child-3 Text");


As you see you can have as many child's and parent's as you want and those can have sub child's of child's and so on....
Hope i help!
 
Share this answer
 
Comments
Orcun Iyigun 25-Aug-11 4:42am    
this question was asked about almost a year ago my friend :)
What you are most likely doing is not specifing the node you want to add the children to. Here is a quick little example I made for you.

C#
TreeNode thisNode;

thisNode = treeView1.Nodes.Add("First Parent");
thisNode.Nodes.AddRange(new TreeNode[]
{
    new TreeNode("Test"),
    new TreeNode("Test #2"),
    new TreeNode("Test #3")
});

thisNode = treeView1.Nodes.Add("Second Parent");
thisNode.Nodes.AddRange(new TreeNode[]
{
    new TreeNode("Test"),
    new TreeNode("Test #2"),
    new TreeNode("Test #3")
});


The first statement simply declares a variable.
The second assigns a new TreeNode object to that variable.
The third adds a bunch of TreeNode objects to the variable, these would be its children.
 
Share this answer
 
Comments
fjdiewornncalwe 22-Nov-10 16:32pm    
Way to do his homework for him...
Agraharapu Nanajee 7-Feb-18 22:08pm    
tewte
fjdiewornncalwe 22-Nov-10 16:36pm    
Didn't mean that comment to sound the way it looks... Your answer is very good, but when someone is looking to have someone else do their homework without showing any effort towards a solution themselves, I get a little snarky... Cheers...
Orcun Iyigun 22-Nov-10 16:37pm    
thanks that worked out really well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900