Background: we have encountered this situation today:
You need to bind part of sitemap content to a Treeview control. The sitemap structure is as follows: <? XML version = "1.0" encoding = "UTF-8" ?>
< Sitemap Xmlns = "Http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
< Sitemapnode URL = "~ /Default. aspx" Title = "Homepage" >
< Sitemapnode URL = "~ /Addreport. aspx" Title = "Video error" > </ Sitemapnode >
< Sitemapnode URL = "~ /Recommand. aspx" Title = "Movie recommendation" > </ Sitemapnode >
< Sitemapnode URL = "" Title = "Starstar movie background" >
< Sitemapnode URL = "" Title = "System functions" Description = "Starstar movie Management System" >
< Sitemapnode URL = "~ /Manager/default. aspx" Roles = "Administrators" Title = "Starstar movie background" > </ Sitemapnode >
< Sitemapnode URL = "~ /Manager/noticemanager. aspx" Title = "Announcement Management" > </ Sitemapnode >
< Sitemapnode URL = "" Title = "Administrator management" >
< Sitemapnode URL = "~ /Manager/addmanager. aspx" Title = "Add an administrator" > </ Sitemapnode >
< Sitemapnode URL = "~ /Manager/managers. aspx" Title = "Administrator" > </ Sitemapnode >
</ Sitemapnode >
</ Sitemapnode >
</ Sitemapnode >
</ Sitemapnode >
</ Sitemap >
For example, I want to bind the content starting from the node with the title "Start star movie background" (hereinafter referred to as the target node) to the Treeview, but do not want to bind the node above it. There is a sitemap. currentnode that can get the current node. Therefore, the problem is converted to locating the target node based on the current node. Finally, a recursive function is used to solve this problem: Private Sitemapnode getbackrootnode (sitemapnode currentnode, String Title)
{
If (Currentnode. Title = Title) Return Currentnode; // If yes, this node is returned.
If (Currentnode = Sitemap. rootnode) Return Currentnode; // If it is the first node, the first node is returned. An exception that does not exist can also be thrown here.
Return Getbackrootnode (currentnode. parentnode, title ); // Find the parent node.
}
A tree is a very special structure. Many of its relationships can be expressed by parent or children. Therefore, many recursive solutions are formed. When encountering tree-related content, consider using recursion. The problem may be easily converted.