Export (0) Print
Expand All

SiteMapNode Class

Represents a node in the hierarchical site map structure such as that described by the SiteMap class and classes that implement the abstract SiteMapProvider class.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

'Declaration
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class SiteMapNode _
	Implements ICloneable, IHierarchyData, INavigateUIData
'Usage
Dim instance As SiteMapNode

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

System.Object
  System.Web.SiteMapNode

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft