日期:2014-05-18  浏览次数:20421 次

根据数据库的值,加载XML的数据到TreeView
C# code
<Root ID="OSMS" >
    <node ID="SMCG" Text="公告管理" ImageUrl="images/info.gif" Expanded="true" src="#">
      <node ID="CGXXGL" Text="发部公告" ImageUrl="images/info.gif" src="Public Notice/PublicNotice_Add.aspx" />
      <node ID="XSXXGL" Text="删除公告" ImageUrl="images/info.gif" src="Public Notice/PublicNotice_Del.aspx" />
      <node ID="XSXXGL" Text="编辑公告" ImageUrl="images/info.gif" src="Public Notice/PublicNoticeEdit.aspx" />
      <node ID="XSXXGL" Text="查询公告" ImageUrl="images/info.gif" src="Public Notice/PublicNoticeView.aspx" />
    <node>
  [code=C#]
[code=C#][/code]</Root>
[/code]

比如我数据库中取出来的值是和Root ID ="OSMS"的ID值一样,就把Note节点的所有数据加载到TreeView这个
怎么实现啊!
下面是目前所实现代码,但我还是认为里面少了一个调用方法,不知写哪里好,请大家给你看看
C# code
 
private void BuildTree()
        {
                      tree_fromXML(Server.MapPath("ManageTree.xml"));
        }

        private void tree_fromXML(string XMLpath)
        {
            System.Xml.XmlDocument document = new System.Xml.XmlDataDocument();
            document.Load(XMLpath);  //xml文件加载到内存
            Build_tree(document.DocumentElement, MyTreeView.Nodes);
        }

        private void Build_tree(System.Xml.XmlNode document, TreeNodeCollection nodes)
        {
            foreach (System.Xml.XmlNode node in document.ChildNodes)
            {
                if (node.Attributes["ID"].Value=="OSMS")
                {
                    if (node.FirstChild.Attributes["ID"].Value == "SMCG")
                    {
                        TreeNode new_child = new TreeNode();
                        new_child.Text = node.FirstChild.Attributes["Text"].Value;
                        if (node.FirstChild.Attributes["ImageUrl"] != null && node.FirstChild.Attributes["ImageUrl"].Value != "")
                            new_child.ImageUrl = node.FirstChild.Attributes["ImageUrl"].Value;
                        if (node.FirstChild.Attributes["src"].Value.Trim() != "#")
                        {
                            //new_child.Target = "frameColumn";
                            new_child.NavigateUrl = node.FirstChild.Attributes["src"].Value;
    &n