SiteMapNode Class

Definition

表示分层站点地图结构中的节点,例如 SiteMap 类和实现抽象 SiteMapProvider 类的类所描述的节点。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.

下面的代码示例演示如何使用 SiteMapNodeCollection 构造函数创建新的 SiteMapNodeCollection 集合,然后使用 Add 方法向其中添加元素。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

下面的代码示例演示了如何 SimpleTextSiteMapProvider 使用以逗号分隔的字符串来分析包含站点地图数据的文本文件。The following code example demonstrates how the SimpleTextSiteMapProvider parses a text file that contains site map data in comma-delimited strings. 新的 SiteMapNode 对象将添加到从该文件读取的每一行的类的内部跟踪集合中。A new SiteMapNode object is added to the internal tracking collections of the class for each line that is read from the file.

此代码示例是为 SiteMapProvider 类提供的更大示例的一部分。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

SiteMapNode 对象表示站点地图结构中的网站页面。A SiteMapNode object represents a Web site page in a site map structure. SiteMapNode 对象由静态 SiteMap 类在运行时通过使用一个或多个站点地图提供程序加载,以将站点地图数据从持久性存储加载到内存中。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 对象由 SiteMapNodeItem 类包装,供 Web 服务器控件(如 SiteMapPath 控件)使用。SiteMapNode objects are wrapped by the SiteMapNodeItem class for use by Web server controls, such as the SiteMapPath control.

SiteMapNode 类包含若干属性,这些属性用于说明网站中的单个页面,包括描述页面的属性,如 UrlTitleDescription 属性。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. 尽管 Url 属性由 XmlSiteMapProvider 类使用,后者是 ASP.NET 的默认站点地图提供程序,作为提供程序用于跟踪节点的内部集合中的查找键,SiteMapNode 类支持一个可供站点地图提供程序用于跟踪节点的基本 Key 属性。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. 此外,导航控件使用 Url 属性,将超链接呈现到导航结构中的页面。Additionally, the Url property is used by navigation controls to render hyperlinks to pages within a navigation structure. Title 属性是 SiteMapNode的友好名称,它通常与 Web 窗体的 HTML 标题相同,并由导航控件用于呈现简单标签。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. 最后,提供其他 Attributes 属性的 NameValueCollection 集合可用于使用 SiteMapNode 对象的站点地图提供程序,但需要在基本 SiteMapNode 类中不可用的其他属性。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)

使用指定的 SiteMapNode 和站点地图提供程序对 key 类的新实例进行初始化,其中,前者用于标识该节点代表的页,后者用于管理该节点。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)

使用以下三项对 SiteMapNode 类的新实例进行初始化:一是指定的 URL;二是一个 key,用于标识节点所代表的页;三是站点地图提供程序,用于管理节点。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)

使用以下四项对 SiteMapNode 类的新实例进行初始化:一是指定的 URL;二是一个 key,用于标识节点所代表的页;三是一个标题;四是站点地图提供程序,用于管理节点。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)

使用以下四项对 SiteMapNode 类的新实例进行初始化:一是指定的 URL;二是一个 key,用于标识节点所代表的页;三是标题和说明;四是站点地图提供程序,用于管理节点。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)

使用管理该节点的指定站点地图提供程序、URL、标题、描述、角色、附加特性以及用于本地化的显式和隐式的资源键来初始化 SiteMapNode 类的新实例。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

除了为 SiteMapNode 类定义的强类型属性以外,获取或设置附加特性的集合。Gets or sets a collection of additional attributes beyond the strongly typed properties that are defined for the SiteMapNode class.

ChildNodes

获取或设置来自关联的SiteMapNode 提供程序的当前 SiteMapProvider 对象的所有子节点。Gets or sets all the child nodes of the current SiteMapNode object from the associated SiteMapProvider provider.

Description

获取或设置 SiteMapNode 的说明。Gets or sets a description for the SiteMapNode.

HasChildNodes

获取一个值,它指示当前 SiteMapNode 是否具有子节点。Gets a value indicating whether the current SiteMapNode has any child nodes.

Item[String]

根据指定的键获取或设置一个来自 Attributes 集合的自定义特性或资源字符串。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

获取与当前节点位于相同层级、相对于 SiteMapNode 属性的下一个 ParentNode 节点(如果存在)。Gets the next SiteMapNode node on the same hierarchical level as the current one, relative to the ParentNode property (if one exists).

ParentNode

获取或设置作为当前节点的父节点的 SiteMapNode 对象。Gets or sets the SiteMapNode object that is the parent of the current node.

PreviousSibling

获取与当前节点位于相同层级、相对于 SiteMapNode 对象的前一个 ParentNode 对象(如果存在)。Gets the previous SiteMapNode object on the same level as the current one, relative to the ParentNode object (if one exists).

Provider

获取跟踪 SiteMapProvider 对象的 SiteMapNode 提供程序。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

获取或设置用于本地化 SiteMapNode 的资源键。Gets or sets the resource key that is used to localize the SiteMapNode.

Roles

获取或设置与 SiteMapNode 对象关联的角色集合,这些角色用于安全修整期间。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. 如果不存在提供程序层次结构,则 RootNode 属性将获取当前提供程序的根节点。If no provider hierarchy exists, the RootNode property gets the root node of the current provider.

Title

获取或设置 SiteMapNode 对象的标题。Gets or sets the title of the SiteMapNode object.

Url

获取或设置 SiteMapNode 对象所代表的页的 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)

获取一个值,该值指示当前的 SiteMapNode 是否与指定对象相同。Gets a value indicating whether the current SiteMapNode is identical to the specified object.

GetAllNodes()

检索属于调用节点的子代的所有 SiteMapNode 对象的只读集合,而不管分离程度如何。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)

检索与当前节点关联的 SiteMapDataSourceView 对象。Retrieves the SiteMapDataSourceView object that is associated with the current node.

GetExplicitResourceString(String, String, Boolean)

检索本地化的字符串,此方法基于以下三个参数:要本地化的 SiteMapNode 特性,未找到资源时使用的默认字符串,以及指示在未找到资源时是否引发异常的布尔值。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()

返回 SiteMapNode 对象的哈希代码。Returns the hash code of the SiteMapNode object.

GetHierarchicalDataSourceView()

检索与当前节点关联的 SiteMapHierarchicalDataSourceView 对象。Retrieves the SiteMapHierarchicalDataSourceView object that is associated with the current node.

GetImplicitResourceString(String)

获取一个本地化的字符串,此方法基于以下信息:跟踪 ResourceKeySiteMapProvider 所指定的特性名称和 SiteMapNode 属性。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()

获取当前实例的 TypeGets 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()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

SiteMapNode 类的此实例的值转换为其等效的字符串表示形式。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. 有关此成员的说明,请参阅 Clone()For a description of this member, see Clone().

IHierarchyData.GetChildren()

检索当前项的分层子数据项。Retrieves the hierarchical children data items of the current item. 有关此成员的说明,请参阅 GetChildren()For a description of this member, see GetChildren().

IHierarchyData.GetParent()

检索当前项的分层父级。Retrieves the hierarchical parent of the current item. 有关此成员的说明,请参阅 GetParent()For a description of this member, see GetParent().

IHierarchyData.HasChildren

获取一个值,该值指示当前 SiteMapNode 对象是否具有任何子节点。Gets a value that indicates whether the current SiteMapNode object has any child nodes. 有关此成员的说明,请参阅 HasChildrenFor a description of this member, see HasChildren.

IHierarchyData.Item

获取分层数据项。Gets the hierarchical data item. 有关此成员的说明,请参阅 ItemFor a description of this member, see Item.

IHierarchyData.Path

获取分层数据项的路径。Gets the path of the hierarchical data item. 有关此成员的说明,请参阅 PathFor a description of this member, see Path.

IHierarchyData.Type

获取表示分层数据项的类型名称的字符串。Gets a string that represents the type name of the hierarchical data item. 有关此成员的说明,请参阅 TypeFor a description of this member, see Type.

INavigateUIData.Description

获取站点地图节点的 Description 属性。Gets the Description property of the site map node. 有关此成员的说明,请参阅 DescriptionFor a description of this member, see Description.

INavigateUIData.Name

获取站点地图节点的 Title 属性。Gets the Title property of the site map node. 有关此成员的说明,请参阅 NameFor a description of this member, see Name.

INavigateUIData.NavigateUrl

获取站点地图节点的 Url 属性。Gets the Url property of the site map node. 有关此成员的说明,请参阅 NavigateUrlFor a description of this member, see NavigateUrl.

INavigateUIData.Value

获取站点地图节点的 Title 属性。Gets the Title property of the site map node. 有关此成员的说明,请参阅 ValueFor a description of this member, see Value.

Applies to

See also