参考资料:http://www.cnblogs.com/codingsilence/archive/2011/04/13/2146562.html
/// <summary>
/// 绑定xml数据到treeview中区
/// </summary>
public void bindXmlToTreeView(XmlNodeList nodeList,TreeNodeCollection treeNode)
{
foreach (XmlNode node in nodeList)
{
XmlElement xe = (XmlElement)node;
TreeNode newTreeNode = new TreeNode();
newTreeNode.Text = xe.GetAttribute("name");
newTreeNode.Name = xe.GetAttribute("value");
treeNode.Add(newTreeNode);
if (node.HasChildNodes)
{
bindXmlToTreeView(node.ChildNodes,newTreeNode.Nodes);
}
} }
使用:
XmlDocument xmlDoc = new XmlDocument();//
XmlNodeList currentXmlNodeList = null;
xmlDoc.Load(baseDir);
currentXmlNodeList = xmlDoc.SelectNodes("/root/GasTree");
bindXmlToTreeView(currentXmlNodeList,treeView1.Nodes);