C#中TreeView绑定到XML

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);
    }
  }

你可能感兴趣的:(xml,String,C#,url)