代码如下
/// <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);
}
}
/// 绑定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();//
xmlDoc.Load(baseDir);
currentXmlNodeList = xmlDoc.SelectNodes("/root/GasTree");
bindXmlToTreeView(currentXmlNodeList,treeView1.Nodes);