当前位置: 代码迷 >> 综合 >> C#中TreeView绑定到XML
  详细解决方案

C#中TreeView绑定到XML

热度:77   发布时间:2024-01-21 05:34:49.0
void databind_treeView()
  {
    //新建个DataSource指向要绑定的文件
    XmlDataSource xds = new XmlDataSource();
    xds.DataFile = Server.MapPath("managerList.xml");
    XmlDocument xmlDocument = xds.GetXmlDocument();
    //把根节点的东东和treeView实例根节点群丢进去递归
    BindXmlToTreeView(xmlDocument.DocumentElement, TreeView1.Nodes);
  }
  void BindXmlToTreeView(XmlNode node, TreeNodeCollection tnc)
  {
    //获得节点字段值
    string strId = node.Attributes["id"].Value;
    string strUrl = node.Attributes["url"].Value;
    tnc.Add(new TreeNode(strText,strUrl));
    foreach (XmlNode n in node.ChildNodes)
    {
      //指向子节点和父节点的子节点群
      BindXmlToTreeView(n, tnc[tnc.Count - 1].ChildNodes);
    }
  }

  相关解决方案