Learning never exhausts the mind
Home >  Coding > C# ASP.Net MVC > Extending the Web Sitemap Xml Document

Published 6th October 2010 by

In this tutorial, we will have a closer look at the web.sitemap document for website navigation and see how to extend it by binding to custom attributes.

The web.sitemap document is an XML format that contains the navigation structure for your site. Various ASP.Net controls use the document to render navigation elements such as breadcrumb trails, navigation menus and trees.

While the standard format may be adequate for most people's needs, some may require additional information to be stored with the page link. This could be the target for the link, a product id, a different description or anything else.

The beauty of the XML format is that it is extensible. That means that it is very easy to add extra information to it.

Let's have a look at a sample sitemap from the previous tutorial.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="Default.aspx" title="Home"  description="This is our homepage">
      <siteMapNode url="about.aspx" title="About Us"  description="This page is all about our company" />
      <siteMapNode url="products.aspx" title="Products"  description="This is our main product page" >
        <siteMapNode url="product1.aspx" title="Sample Product 1"  description="This is a sample product" />
        <siteMapNode url="product2.aspx" title="Sample Product 2"  description="This is a sample product" />
      <siteMapNode url="services.aspx" title="Our Services"  description="These are the services we provide" />
      <siteMapNode url="contact.aspx" title="Contact Us"  description="How to contact us">
        <siteMapNode url="map.aspx" title="How to Find Us"  description="A Map of how to find us" />

Each SiteMapNode has three elements. A URL, title and a description. To add a product id to the nodes is as easy as changing the XML, so the two products become:

        <siteMapNode url="product1.aspx" title="Sample Product 1"  description="This is a sample product" prodID="PROD001" />
        <siteMapNode url="product2.aspx" title="Sample Product 2"  description="This is a sample product" prodID="PROD002" />

Now all we need is some way of getting this value on the products page. The easiest way is to use the SiteMap class indexer to access the attributes:

string prodID = SiteMap.CurrentNode["prodID"]

In the previous tutorial we saw how to add sitemap information to the aspx page using an eval:

<%= SiteMap.CurrentNode.Title %>

And you can do the same thing with the attribute as well.

<%= SiteMap.CurrentNode["prodID"] %>

Easy. Now for the harder part. Binding new attributes to existing navigation controls. For this, we need to create a function to attach to the DataBound event of a control. In this example, I am going to append the prodID to the end of the title on the TreeView control. This method can be adapted for use with any of the navigation controls and properties.

First, start with a basic sitemap navigation tree view and data source and link the OnTreeNodeDataBound event to a function called TreeView1_TreeNodeDataBound.

<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" OnTreeNodeDataBound="TreeView1_TreeNodeDataBound" >
<asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server"></asp:SiteMapDataSource>

In the C# code behind, create a function called TreeView1_TreeNodeDataBound with the following code.

protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    SiteMapNode node = e.Node.DataItem as SiteMapNode;
    e.Node.Text = string.Format("{0} - {1}", node.Title, node["prodID"]);

This illustrates how to access the default properties and custom attributes for the sitemap nodes and how to bind them to custom properties of a control.

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.