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.

System.Object
  System.Web.SiteMapNode

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

public class SiteMapNode : ICloneable, 
	IHierarchyData, INavigateUIData

The SiteMapNode type exposes the following members.

  NameDescription
Public methodSiteMapNode(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.
Public methodSiteMapNode(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.
Public methodSiteMapNode(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.
Public methodSiteMapNode(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.
Public methodSiteMapNode(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.
Top

  NameDescription
Protected propertyAttributesGets or sets a collection of additional attributes beyond the strongly typed properties that are defined for the SiteMapNode class.
Public propertyChildNodesGets or sets all the child nodes of the current SiteMapNode object from the associated SiteMapProvider provider.
Public propertyDescriptionGets or sets a description for the SiteMapNode.
Public propertyHasChildNodesGets a value indicating whether the current SiteMapNode has any child nodes.
Public propertyItemGets or sets a custom attribute from the Attributes collection or a resource string based on the specified key.
Public propertyKeyGets a string representing a lookup key for a site map node.
Public propertyNextSiblingGets the next SiteMapNode node on the same hierarchical level as the current one, relative to the ParentNode property (if one exists).
Public propertyParentNodeGets or sets the SiteMapNode object that is the parent of the current node.
Public propertyPreviousSiblingGets the previous SiteMapNode object on the same level as the current one, relative to the ParentNode object (if one exists).
Public propertyProviderGets the SiteMapProvider provider that the SiteMapNode object is tracked by.
Public propertyReadOnlyGets or sets a value indicating whether the site map node can be modified.
Public propertyResourceKeyGets or sets the resource key that is used to localize the SiteMapNode.
Public propertyRolesGets or sets a collection of roles that are associated with the SiteMapNode object, used during security trimming.
Public propertyRootNodeGets 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.
Public propertyTitleGets or sets the title of the SiteMapNode object.
Public propertyUrlGets or sets the URL of the page that the SiteMapNode object represents.
Top

  NameDescription
Public methodClone()Creates a new node that is a copy of the current node.
Public methodClone(Boolean)Creates a new copy that is a copy of the current node, optionally cloning all parent and ancestor nodes of the current node.
Public methodEqualsGets a value indicating whether the current SiteMapNode is identical to the specified object. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAllNodesRetrieves a read-only collection of all SiteMapNode objects that are descendants of the calling node, regardless of the degree of separation.
Public methodGetDataSourceViewRetrieves the SiteMapDataSourceView object that is associated with the current node.
Protected methodGetExplicitResourceStringRetrieves 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.
Public methodGetHashCodeReturns the hash code of the SiteMapNode object. (Overrides Object.GetHashCode().)
Public methodGetHierarchicalDataSourceViewRetrieves the SiteMapHierarchicalDataSourceView object that is associated with the current node.
Protected methodGetImplicitResourceStringGets a localized string based on the attribute name and ResourceKey property that is specified by the SiteMapProvider by which the SiteMapNode is tracked.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsAccessibleToUserGets a value indicating whether the specified site map node can be viewed by the user in the specified context.
Public methodIsDescendantOfGets a value indicating whether the current site map node is a child or a direct descendant of the specified node.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringConverts the value of this instance of the SiteMapNode class to its equivalent string representation. (Overrides Object.ToString().)
Top

  NameDescription
Explicit interface implemetationPrivate methodICloneable.CloneInfrastructure. Creates a new node that is a copy of the current node. For a description of this member, see ICloneable.Clone.
Explicit interface implemetationPrivate methodIHierarchyData.GetChildrenRetrieves the hierarchical children data items of the current item. For a description of this member, see IHierarchyData.GetChildren.
Explicit interface implemetationPrivate methodIHierarchyData.GetParentRetrieves the hierarchical parent of the current item. For a description of this member, see IHierarchyData.GetParent.
Explicit interface implemetationPrivate propertyIHierarchyData.HasChildrenGets a value that indicates whether the current SiteMapNode object has any child nodes. For a description of this member, see IHierarchyData.HasChildren.
Explicit interface implemetationPrivate propertyIHierarchyData.ItemGets the hierarchical data item. For a description of this member, see IHierarchyData.Item.
Explicit interface implemetationPrivate propertyIHierarchyData.PathGets the path of the hierarchical data item. For a description of this member, see IHierarchyData.Path.
Explicit interface implemetationPrivate propertyIHierarchyData.TypeGets a string that represents the type name of the hierarchical data item. For a description of this member, see IHierarchyData.Type.
Explicit interface implemetationPrivate propertyINavigateUIData.DescriptionGets the Description property of the site map node. For a description of this member, see INavigateUIData.Description.
Explicit interface implemetationPrivate propertyINavigateUIData.NameGets the Title property of the site map node. For a description of this member, see INavigateUIData.Name.
Explicit interface implemetationPrivate propertyINavigateUIData.NavigateUrlGets the Url property of the site map node. For a description of this member, see INavigateUIData.NavigateUrl.
Explicit interface implemetationPrivate propertyINavigateUIData.ValueGets the Title property of the site map node. For a description of this member, see INavigateUIData.Value.
Top

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() 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 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 &&
                (null == nodeValues[3] || nodeValues[3] == String.Empty))
            {
              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;
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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