读xml文件生成树

时间:2022-07-01 13:19:07

      你去建两张表把这目录做成树的形式吧,这有个例子给你。

      我把那树的结构写在xml中行不行?

      行,你自己看着办。

      靠,每次都是这样让我拿着那半成品在改,真是受不了。开始又不把它写成树的形式现在来让我改,无语。被这个纠结的项目整的郁闷死了。今天又提出新的需求了,领导要看到系统在线的人数,还要了解他们在用那些应用。citrix中也不知道有没有给出接口,研究了一上午得出的结论却不容乐观,citrix上给出的性能方面的数据却不是我想要的,杯具啊。

      这是我建的一个xml的树形结构

 

<?xml version="1.0" encoding="utf-8" ?>

<Tree>

  <parentNode name="父节点1" icon="~/images/dw.gif" url="flgl/ClassManager.aspx" target="iFrameInfo"></parentNode>

  <parentNode name="父节点2" icon="~/images/dw.gif" url="flgl/Sec_class.aspx" target="iFrameInfo"></parentNode>

  <parentNode name="父节点3" icon="~/images/dw.gif" url="AppIconList/AppIconList.aspx" target="iFrameInfo"></parentNode>

  <parentNode name="父节点4" icon="~/images/dw.gif" url="">

    <childNode name="子节点1" icon="~/images/dw.gif" url="ipgl/ServerAdressManager.aspx" target="iFrameInfo"></childNode>

    <childNode name="子节点2" icon="~/images/dw.gif" url="ipgl/ZDCAdressManager.aspx" target="iFrameInfo"></childNode>

  </parentNode>

  <parentNode name="父节点5" icon="~/images/dw.gif" url="AppList.aspx" target="_parent"></parentNode>

</Tree>

 

我现在要做的是将他的结构写到树里去,下面是读取xml和将它添加到树中去。

   //循环他的所有节点并添加到树中去

 

    protected void CreateTree()

    {

 

        XmlDocument xmlDoc = new XmlDocument();

        string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "tree.xml";

        xmlDoc.Load(xmlPath);

        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Tree").ChildNodes;

        //XmlNodeList nodeList = xmlDoc.SelectNodes("@name[//parentNode]");

        //TreeNode rootNode = new TreeNode("geng");

        //TreeView1.Nodes.Add(rootNode);

        try

        {

            TreeView1.Nodes.Clear();//清除树控件中所有节点

            foreach (XmlNode xn in nodeList)

            {

                XmlElement xe = (XmlElement)xn;

                string str = xe.GetAttribute("name");

                string strIcon = xe.GetAttribute("icon");

                string strUrl = xe.GetAttribute("url");

                string strTarget = xe.GetAttribute("target");

                TreeNode tn = new TreeNode(str);

                tn.ImageUrl = strIcon;

                if (!strUrl.Equals(""))

                {

                    tn.NavigateUrl = strUrl;

                }

                tn.Target = strTarget;

                TreeView1.Nodes.Add(tn);

 

                XmlNodeList childNodeList = xn.ChildNodes;

                foreach (XmlNode cxn in childNodeList)

                {

                    XmlElement cxe = (XmlElement)cxn;

                    string str2 = cxe.GetAttribute("name");

                    string strUrl2 = cxe.GetAttribute("url");

                    string strTarget2 = cxe.GetAttribute("target");

                    TreeNode tnChild = new TreeNode(str2, str2);

                    if(!strUrl2.Equals(""))

                    {

                        tnChild.NavigateUrl = strUrl2;

                    }

                    tnChild.Target = strTarget2;

                    tn.ChildNodes.Add(tnChild);

                }

            }

            TreeView1.CollapseAll();

        }

        catch

        {

 

        }

    }

 

    //用递归方法循环xml中的节点生成树

    protected void CreateNewTree()

    {

        XmlDocument xmlDoc = new XmlDocument();

        string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "tree.xml";

        xmlDoc.Load(xmlPath);

        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Tree").ChildNodes;

        TreeView1.Nodes.Clear();//清除树控件中所有节点

        try

        {

            foreach (XmlNode xn in nodeList)

            {

                XmlElement xe = (XmlElement)xn;

                string str = xe.GetAttribute("name");

                string strIcon = xe.GetAttribute("icon");

                string strUrl = xe.GetAttribute("url");

                string strTarget = xe.GetAttribute("target");

                TreeNode tn = new TreeNode(str);

                tn.ImageUrl = strIcon;

                if (!strUrl.Equals(""))

                {

                    tn.NavigateUrl = strUrl;

                }

                tn.Target = strTarget;

                TreeView1.Nodes.Add(tn);

 

                XmlNodeList childNodeList = xn.ChildNodes;

                bianliAllNode(tn, childNodeList);

            }

        }

        catch

        {

 

        }

        TreeView1.CollapseAll();

    }

 

    //递归循环所有节点

    private void bianliAllNode(TreeNode rtn, XmlNodeList nodeList) 

    {

        try

        {

            foreach (XmlNode xn in nodeList)

            {

                XmlElement xe = (XmlElement)xn;

                string str = xe.GetAttribute("name");

                string strIcon = xe.GetAttribute("icon");

                string strUrl = xe.GetAttribute("url");

                string strTarget = xe.GetAttribute("target");

                TreeNode tn = new TreeNode(str);

                tn.ImageUrl = strIcon;

                if (!strUrl.Equals(""))

                {

                    tn.NavigateUrl = strUrl;

                }

                tn.Target = strTarget;

                rtn.ChildNodes.Add(tn);

 

                XmlNodeList childNodeList = xn.ChildNodes;

                bianliAllNode(tn, childNodeList);

            }

        }

        catch

        {

 

        }

    }

页面中的代码:

 

<asp:TreeView ID="TreeView1" runat="server" ImageSet="WindowsHelp" NodeIndent="10" Width="149px" ShowLines="True" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" Height="79px">

                                        <ParentNodeStyle Font-Bold="True" ForeColor="black" ImageUrl="~/images/dw.gif" />

                                        <HoverNodeStyle Font-Underline="False" />

                                        <SelectedNodeStyle Font-Underline="false" HorizontalPadding="0px" VerticalPadding="0px" Font-Bold="false" Font-Italic="false" ForeColor="black" />

                                        <Nodes>

                                            <asp:TreeNode Text="节点一" Value="节点一" Expanded="true">

                                                <asp:TreeNode Text="一队" Value="一队" Expanded="True">

                                                    <asp:TreeNode Text="张三" Value="张三" NavigateUrl="" Target="iFrameInfo"></asp:TreeNode>

                                                    <asp:TreeNode Text="李四" Value="李四"></asp:TreeNode>

                                                </asp:TreeNode>

                                                <asp:TreeNode Text="二队" Value="二队" PopulateOnDemand="True"></asp:TreeNode>

                                            </asp:TreeNode>

                                        </Nodes>

                                        <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"

                                            NodeSpacing="0px" VerticalPadding="0px" ImageUrl="~/images/person.gif" />

                                        <RootNodeStyle ImageUrl="~/images/dw.gif" />

                                    </asp:TreeView>