Asp.net TreeView and XML generate a list tree in three steps

Source: Internet
Author: User

I just found out when I was doing Tree view binding. Some people on the Internet say that TreeView is used to bind data sources and recursively bind data sources. If I don't want to see it, I tried to write
I did this. If you have any questions, please instruct me. I am a cainiao ..
1: Page TreeView is called tvFunction
2: Create adminFunction. xml
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Functions>
<Function name = "product release" url = "~ /Page/leaves/leave. aspx ">
<Subfunction name = "add product" url = "~ /Page/leaves/leave. aspx ">
</Subfunction>
<Subfunction name = "add product" url = "~ /Page/leaves/leave. aspx ">
</Subfunction>
</Function>
<Function name = "message publishing" url = "~ /Page/leaves/leave. aspx ">
<Subfunction name = "add message" url = "~ /Page/leaves/leave. aspx ">
</Subfunction>
<Subfunction name = "Modify message" url = "~ /Page/leaves/leave. aspx ">
</Subfunction>
</Function>
</Functions>

3: Page binding
Copy codeThe Code is as follows:
/// <Summary>
/// Initialize the function tree
/// </Summary>
Private void InitFunction ()
{
XmlDocument xmldoc = new XmlDocument ();
String xmlPath = "~ /AdminFunction. xml ";
XmlPath = Server. MapPath (xmlPath );
Xmldoc. Load (xmlPath );
XmlNode root = xmldoc. DocumentElement;

Foreach (XmlNode node in root. ChildNodes)
{
// Obtain the information of the parent node (frivolous-scholar)
XmlElement x1 = node as XmlElement;
String name = x1.GetAttribute ("name ");
String url = x1.GetAttribute ("url ");
TreeNode tn = new TreeNode ();
Tn. Text = name;
Tn. NavigateUrl = url;
Foreach (XmlNode subnode in x1.ChildNodes)
{
XmlElement subxmlEmt = subnode as XmlElement;
String subname = subxmlEmt. GetAttribute ("name ");
String suburl = subxmlEmt. GetAttribute ("url ");
TreeNode subtn = new TreeNode ();
Subtn. Text = subname;
Subtn. NavigateUrl = suburl;
Tn. ChildNodes. Add (subtn );


}
TvFunction. Nodes. Add (tn );

}
}

4: Display Effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.