SiteMapNode Class

Definition

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.

public ref class SiteMapNode : ICloneable, System::Web::UI::IHierarchyData, System::Web::UI::INavigateUIData
public class SiteMapNode : ICloneable, System.Web.UI.IHierarchyData, System.Web.UI.INavigateUIData
type SiteMapNode = class
    interface ICloneable
    interface IHierarchyData
    interface INavigateUIData
Public Class SiteMapNode
Implements ICloneable, IHierarchyData, INavigateUIData
Inheritance
SiteMapNode
Implements

Examples

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() method loads site navigation
// data from persistent storage into a DataTable.
DataTable siteMap = LoadSiteMapData();

// Create a SiteMapNodeCollection.
SiteMapNodeCollection nodes = new SiteMapNodeCollection();

// Create a SiteMapNode and add it to the collection.
SiteMapNode tempNode;
DataRow row;
int index = 0;

while (index < siteMap.Rows.Count)
{

    row = siteMap.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;
}
' 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 virtual void LoadSiteMapFromStore()
{
  string pathToOpen;

  lock (this)
  {
    // If a root node exists, LoadSiteMapFromStore has already
    // been called, and the method can return.
    if (rootNode != null)
    {
      return;
    }
    else
    {
      pathToOpen = HttpContext.Current.Server.MapPath("~" + "\\" + sourceFilename);

      if (File.Exists(pathToOpen))
      {
        // Open the file to read from.
        using (StreamReader sr = File.OpenText(pathToOpen))
        {

          // Clear the state of the collections and rootNode
          rootNode = null;
          siteMapNodes.Clear();
          childParentRelationship.Clear();

          // Parse the file and build the site map
          string s = "";
          string[] nodeValues = null;
          SiteMapNode temp = null;

          while ((s = sr.ReadLine()) != null)
          {

            // Build the various SiteMapNode objects and add
            // them to the ArrayList collections. The format used
            // is: URL,TITLE,DESCRIPTION,PARENTURL

            nodeValues = s.Split(',');

            temp = new SiteMapNode(this,
                HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                nodeValues[1],
                nodeValues[2]);

            // Is this a root node yet?
            if (null == rootNode &&
                string.IsNullOrEmpty(nodeValues[3]))
            {
              rootNode = 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.
              SiteMapNode parentNode =
                       FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[3]);
              if (parentNode != null)
              {
                childParentRelationship.Add(new DictionaryEntry(temp.Url, parentNode));
              }
              else
              {
                throw new Exception("Parent node not found for current node.");
              }
            }
          }
        }
      }
      else
      {
        throw new Exception("File not found");
      }
    }
  }
  return;
}
  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
End Class

Remarks

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.

Constructors

SiteMapNode(SiteMapProvider, String)

Initializes a new instance of the SiteMapNode class, using the specified key to identify the page that the node represents and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, a title, and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, a title and description, and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String, String, String, IList, NameValueCollection, NameValueCollection, String)

Initializes a new instance of the SiteMapNode class using the specified site map provider that manages the node, URL, title, description, roles, additional attributes, and explicit and implicit resource keys for localization.

Properties

Attributes

Gets or sets a collection of additional attributes beyond the strongly typed properties that are defined for the SiteMapNode class.

ChildNodes

Gets or sets all the child nodes of the current SiteMapNode object from the associated SiteMapProvider provider.

Description

Gets or sets a description for the SiteMapNode.

HasChildNodes

Gets a value indicating whether the current SiteMapNode has any child nodes.

Item[String]

Gets or sets a custom attribute from the Attributes collection or a resource string based on the specified key.

Key

Gets a string representing a lookup key for a site map node.

NextSibling

Gets the next SiteMapNode node on the same hierarchical level as the current one, relative to the ParentNode property (if one exists).

ParentNode

Gets or sets the SiteMapNode object that is the parent of the current node.

PreviousSibling

Gets the previous SiteMapNode object on the same level as the current one, relative to the ParentNode object (if one exists).

Provider

Gets the SiteMapProvider provider that the SiteMapNode object is tracked by.

ReadOnly

Gets or sets a value indicating whether the site map node can be modified.

ResourceKey

Gets or sets the resource key that is used to localize the SiteMapNode.

Roles

Gets or sets a collection of roles that are associated with the SiteMapNode object, used during security trimming.

RootNode

Gets the root node of the root provider in a site map provider hierarchy. If no provider hierarchy exists, the RootNode property gets the root node of the current provider.

Title

Gets or sets the title of the SiteMapNode object.

Url

Gets or sets the URL of the page that the SiteMapNode object represents.

Methods

Clone()

Creates a new node that is a copy of the current node.

Clone(Boolean)

Creates a new copy that is a copy of the current node, optionally cloning all parent and ancestor nodes of the current node.

Equals(Object)

Gets a value indicating whether the current SiteMapNode is identical to the specified object.

GetAllNodes()

Retrieves a read-only collection of all SiteMapNode objects that are descendants of the calling node, regardless of the degree of separation.

GetDataSourceView(SiteMapDataSource, String)

Retrieves the SiteMapDataSourceView object that is associated with the current node.

GetExplicitResourceString(String, String, Boolean)

Retrieves a localized string based on a SiteMapNode attribute to localize, a default string to return if no resource is found, and a Boolean value indicating whether to throw an exception if no resource is found.

GetHashCode()

Returns the hash code of the SiteMapNode object.

GetHierarchicalDataSourceView()

Retrieves the SiteMapHierarchicalDataSourceView object that is associated with the current node.

GetImplicitResourceString(String)

Gets a localized string based on the attribute name and ResourceKey property that is specified by the SiteMapProvider by which the SiteMapNode is tracked.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsAccessibleToUser(HttpContext)

Gets a value indicating whether the specified site map node can be viewed by the user in the specified context.

IsDescendantOf(SiteMapNode)

Gets a value indicating whether the current site map node is a child or a direct descendant of the specified node.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Converts the value of this instance of the SiteMapNode class to its equivalent string representation.

Explicit Interface Implementations

ICloneable.Clone()

Creates a new node that is a copy of the current node. For a description of this member, see Clone().

IHierarchyData.GetChildren()

Retrieves the hierarchical children data items of the current item. For a description of this member, see GetChildren().

IHierarchyData.GetParent()

Retrieves the hierarchical parent of the current item. For a description of this member, see GetParent().

IHierarchyData.HasChildren

Gets a value that indicates whether the current SiteMapNode object has any child nodes. For a description of this member, see HasChildren.

IHierarchyData.Item

Gets the hierarchical data item. For a description of this member, see Item.

IHierarchyData.Path

Gets the path of the hierarchical data item. For a description of this member, see Path.

IHierarchyData.Type

Gets a string that represents the type name of the hierarchical data item. For a description of this member, see Type.

INavigateUIData.Description

Gets the Description property of the site map node. For a description of this member, see Description.

INavigateUIData.Name

Gets the Title property of the site map node. For a description of this member, see Name.

INavigateUIData.NavigateUrl

Gets the Url property of the site map node. For a description of this member, see NavigateUrl.

INavigateUIData.Value

Gets the Title property of the site map node. For a description of this member, see Value.

Applies to

See also