private void Form1_Load(object sender, EventArgs e) { var doc = XDocument.Load("data.xml"); var root = doc.Root; var x = GetNodes(new TreeNode(root.Name.LocalName), root).ToArray(); treeView1.Nodes.AddRange(x); } private IEnumerable<TreeNode> GetNodes(TreeNode node, XElement element) { return element.HasElements ? node.AddRange(from item in element.Elements() let tree = new TreeNode(item.Name.LocalName) from newNode in GetNodes(tree, item) select newNode) : new[] { node }; } public static class TreeNodeEx { public static IEnumerable<TreeNode> AddRange(this TreeNode collection, IEnumerable<TreeNode> nodes) { var items = nodes.ToArray(); collection.Nodes.AddRange(items); return new[] { collection }; } }
http://blog.csdn.net/sfcyyc/article/details/7448422
http://stackoverflow.com/questions/5437713/binding-hierarchical-xml-to-treeview
递归啊 少年!!
哦,谢谢。我是初学者。递归还不会
@漫天de光: ....这个最简单了。
简单的思路就是 方法里面调用方法本身
public void foo(xml){
foreach (child in xml){
tree.add(child)//加载到TreeView
if(child.有子节点)
{
foo(child);
}
}
}