110,533
社区成员
发帖
与我相关
我的任务
分享
private void XmlOperation_Load(object sender, EventArgs e)
{
path = AppDomain.CurrentDomain.BaseDirectory + @"NameList.xml";
xml.Load(path);//加载xml文件
bindTvXml();
}
/// <summary>
/// 绑定TreeView
/// </summary>
private void bindTvXml()
{
for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
{
XmlNode Xnode = xml.DocumentElement.ChildNodes[i];
TreeNode node = new TreeNode();
node.Text = Xnode.Attributes["name"].Value;
node.Tag = Xnode;
bindChildNode(node, Xnode);//绑定子节点
TvXml.Nodes.Add(node);
TvXml.HideSelection = false;
}
}
/// <summary>
/// 递归绑定子节点
/// </summary>
/// <param name="node"></param>
/// <param name="xml"></param>
private void bindChildNode(TreeNode node, XmlNode xml)
{
for (int i = 0; i < xml.ChildNodes.Count; i++)
{
TreeNode Childnode = new TreeNode();
XmlNode ChildXml = xml.ChildNodes[i];
Childnode.Text = ChildXml.Value;
Childnode.Name = "1";
Childnode.Tag = xml.ChildNodes[i];
if (ChildXml.HasChildNodes)
{
if (ChildXml.ChildNodes[0].NodeType == XmlNodeType.Text)
Childnode.Text = ChildXml.ChildNodes[0].InnerText;
else
bindChildNode(Childnode, ChildXml);
}
node.Nodes.Add(Childnode);
}
}