利用递归解析Xml文档添加到TreeView中
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("School.xml");
var root = doc.DocumentElement as XmlNode;
foreach (XmlElement item in root.ChildNodes)
{
TreeNode node = new TreeNode(item.Attributes["name"].Value);
this.treeView1.Nodes.Add(node);
AddTreeNode(item.ChildNodes,node);
}
}
/// <summary>
/// 利用递归解析Xml文档添加到TreeView中
/// </summary>
/// <param name="dataList">Xml节点集合</param>
/// <param name="treeNode">TreeNode节点</param>
public void AddTreeNode(XmlNodeList dataList,TreeNode treeNode)
{
TreeNode node = null;
if (dataList.Count <= 0) return;
foreach (XmlNode item in dataList)
{
node = new TreeNode(item.Attributes["name"].Value);
treeNode.Nodes.Add(node);
AddTreeNode(item.ChildNodes,node);
}
}