private void tree_Bind()
{
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("XMLFile.xml"));
XmlNodeList nodelist = xd.SelectSingleNode("ceshi").ChildNodes;
foreach (XmlNode xnode in nodelist[0].ChildNodes)
{
TreeNode node = new TreeNode();
node.Text = xnode.Attributes[0].Value;
dgTreeBind(node, xnode);
TreeView1.Nodes.Add(node);
}
}
private void dgTreeBind(TreeNode prenode, XmlNode xnode)
{
if (xnode.ChildNodes.Count > 0)
{
foreach (XmlNode childNode in xnode.ChildNodes)
{
TreeNode node = new TreeNode();
node.Text = childNode.Attributes[0].Value;
prenode.ChildNodes.Add(node);
dgTreeBind(node, childNode);
}
}
}
#endregion
读取的XML
<?xml version="1.0" encoding="utf-8" ?>
<ceshi>
<test>
<person name="1" id="001" pre="">
<person name="2" id="002" pre="001"></person>
<person name="3" id="003" pre="001"></person>
</person>
<person name="2" id="004" pre="">
<person name="4" id="005" pre="004"></person>
</person>
<person name="3" id="006" pre="">
<person name="5" id="007" pre="006"></person>
</person>
</test>
</ceshi>