SiteMapNode Class
Assembly: System.Web (in system.web.dll)
'Declaration Public Class SiteMapNode Implements ICloneable, IHierarchyData, INavigateUIData 'Usage Dim instance As SiteMapNode
public class SiteMapNode implements ICloneable, IHierarchyData, INavigateUIData
public class SiteMapNode implements ICloneable, IHierarchyData, INavigateUIData
Not applicable.
A SiteMapNode object represents a Web site page in a site map structure. SiteMapNode objects are loaded by the static SiteMap class at run time using one or more site map providers to load site map data from persistent storage into memory. SiteMapNode objects are wrapped by the SiteMapNodeItem class for use by Web server controls, such as the SiteMapPath control.
The SiteMapNode class includes several properties that are used to describe a single page in a Web site, including properties that describe a page, such as the Url, Title, and Description properties. Whereas the Url property is used by the XmlSiteMapProvider class, which is the default site map provider for ASP.NET, as a lookup key in the internal collections that the provider uses to track nodes, the SiteMapNode class supports a basic Key property that can be used by site map providers to track nodes. Additionally, the Url property is used by navigation controls to render hyperlinks to pages within a navigation structure. The Title property is a friendly name for the SiteMapNode, is often the same as the HTML title of a Web Form, and is used by navigation controls to render simple labels. Finally, a NameValueCollection collection of additional Attributes attributes is available to site map providers that use SiteMapNode objects, but require additional properties that are not available in the base SiteMapNode class.
This section contains two code examples. The first code example demonstrates how to create a new site map node collection and add elements to it. The second code example demonstrates how to load site map data from a text file.
The following code example demonstrates how to use the SiteMapNodeCollection constructor to create a new SiteMapNodeCollection collection, and then add elements to it with the Add method.
' The LoadSiteMapData() Function loads site navigation ' data from persistent storage into a DataTable. Dim siteMapData As DataTable siteMapData = LoadSiteMapData() ' Create a SiteMapNodeCollection. Dim nodes As New SiteMapNodeCollection() ' Create a SiteMapNode and add it to the collection. Dim tempNode As SiteMapNode Dim row As DataRow Dim index As Integer index = 0 While (index < siteMapData.Rows.Count) row = siteMapData.Rows(index) ' Create a node based on the data in the DataRow. tempNode = New SiteMapNode(SiteMap.Provider, row("Key").ToString(), row("Url").ToString()) ' Add the node to the collection. nodes.Add(tempNode) index = index + 1 End While
The following code example demonstrates how the SimpleTextSiteMapProvider parses a text file that contains site map data in comma-delimited strings. A new SiteMapNode object is added to the internal tracking collections of the class for each line that is read from the file.
This code example is part of a larger example provided for the SiteMapProvider class.
Protected Overridable Sub LoadSiteMapFromStore() Dim pathToOpen As String SyncLock Me ' If a root node exists, LoadSiteMapFromStore has already ' been called, and the method can return. If Not (aRootNode Is Nothing) Then Return Else pathToOpen = HttpContext.Current.Server.MapPath("~" & "\\" & sourceFilename) If File.Exists(pathToOpen) Then ' Open the file to read from. Dim sr As StreamReader = File.OpenText(pathToOpen) Try ' Clear the state of the collections and aRootNode aRootNode = Nothing siteMapNodes.Clear() childParentRelationship.Clear() ' Parse the file and build the site map Dim s As String = "" Dim nodeValues As String() = Nothing Dim temp As SiteMapNode = Nothing Do s = sr.ReadLine() If Not s Is Nothing Then ' Build the various SiteMapNode objects and add ' them to the ArrayList collections. The format used ' is: URL,TITLE,DESCRIPTION,PARENTURL nodeValues = s.Split(","c) temp = New SiteMapNode(Me, _ HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _ HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _ nodeValues(1), _ nodeValues(2)) ' Is this a root node yet? If aRootNode Is Nothing AndAlso _ (nodeValues(3) Is Nothing OrElse _ nodeValues(3) = String.Empty) Then aRootNode = temp ' If not the root node, add the node to the various collections. Else siteMapNodes.Add(New DictionaryEntry(temp.Url, temp)) ' The parent node has already been added to the collection. Dim parentNode As SiteMapNode = _ FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(3)) If Not (parentNode Is Nothing) Then childParentRelationship.Add(New DictionaryEntry(temp.Url, parentNode)) Else Throw New Exception("Parent node not found for current node.") End If End If End If Loop Until s Is Nothing Finally sr.Close() End Try Else Throw New Exception("File not found") End If End If End SyncLock Return End Sub 'LoadSiteMapFromStore End Class 'SimpleTextSiteMapProvider
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.