Treeview树形控件
XmlDocument doc = new XmlDocument();
string xml = Application.StartupPath + "\\YQPathConfig\\舆情20121022.xml";
doc.Load(xml);
XmlNode no = doc.DocumentElement;
TreeNode tree = null; ;
foreach(XmlNode node in no.ChildNodes)
{
tree = new TreeNode();
tree.Text = node.Attributes["name"].Value;
this.treeView1.Nodes.Add(tree);
foreach(XmlNode node1 in node.ChildNodes)
{
int i = 0;
TreeNode no1 = new TreeNode();
no1.Text = node1.Attributes["name"].Value;
//this.treeView1.Nodes["" + tree.Text + ""].Nodes.Add(no1);
this.treeView1.Nodes[i].Nodes.Add(no1);
foreach(XmlNode node2 in node1.ChildNodes)
{
TreeNode no2 = new TreeNode();
no2.Text = node2.Attributes["name"].Value;
this.treeView1.Nodes[i].Nodes.Add(no2);
}
i++;
}
}
这句该怎么写this.treeView1.Nodes[i].Nodes.Add(no2); 主要是索引
------最佳解决方案--------------------
this.treeView1.Nodes[node1.Name].Nodes.Add(no2);//node1.Name可能有重复
建议改成for循环,用索引。