ASP.NET 4.0 : Data Source Components (part 9) - The SiteMapDataSource Class - Displaying Site Map Information
6. The SiteMapDataSource Class
Site maps are a common feature of cutting-edge Web sites. A site map is the graph that represents all the pages and directories found in a Web site. Site map information is used to show users the logical coordinates of the page they are visiting, allow users to access site locations dynamically, and render all the navigation information in a graphical fashion (as shown in Figure 3).
Figure 3. The graphical layout that the MSDN Magazine Web site uses to represent the location of a page in the site hierarchy.
ASP.NET contains a rich navigation infrastructure that allows developers to specify the site structure. I cover site navigation in detail in Programming Microsoft ASP.NET 2.0 Applications: Advanced Topics (Microsoft Press, 2006). For now, it suffices to say that the site map is a hierarchical piece of information that can be used as input for a hierarchical data source control such as SiteMapDataSource. The output of SiteMapDataSource can be bound to hierarchical data-bound controls such as Menu.
6.1 Displaying Site Map Information
The sitemap information can appear in many ways, the simplest of which is to place an XML file named web.sitemap in the root of the application. To give you the essence of site maps and site map data sources, let’s briefly review a few usage scenarios. Suppose you’re writing a Web site and your client asks for a sequence of hyperlinks that indicates the location of the page in the site map. In ASP.NET 1.x, you have to create your own infrastructure to hold site map information and render the page location. (Typically, you would use a configuration file and a user control.) Starting with version 2.0, ASP.NET provides richer support for site maps. You start by creating a configuration file named web.sitemap in the root of the Web application. The file describes the relationship between pages on the site. Your next step depends on the expected output.
If the common representation shown in Figure 3 (a sequence of hyperlinks with a separator) is what you need, add a SiteMapPath control to the page. This control retrieves the site map and produces the necessary HTML markup. In this simple case, there is no need to resort to a site map data source control. If you need to build a more complex hierarchical layout—for example, a tree-based representation—you need the SiteMapDataSource control.
The SiteMapDataSource control pumps site map information to a hierarchical data-bound control (for example, the new TreeView control) so that it can display the site’s structure. Here’s a quick example:
<%@ Page Language="C#" %>
<html>
<body>
<form runat="server">
<asp:SiteMapDataSource runat="server" ID="MySiteMapSource" />
<asp:TreeView runat="server" DataSourceId="MySiteMapSource" />
</form>
</body>
</html>
Figure 4 shows the final output as it appears to the end user.
Figure 4. The site map information rendered through a TreeView control.

The site map information might look like the following:
<siteMap>
<siteMapNode title="Home" url="default.aspx" >
<siteMapNode title="Acknowledgements" url="ack.aspx"/>
<siteMapNode title="References" url="ref.aspx" />
<siteMapNode title="Samples">
<siteMapNode title="Part 1">
<siteMapNode title="Chapter 1" />
<siteMapNode title="Chapter 2" />
<siteMapNode title="Chapter 3">
<siteMapNode title="Dynamic Controls"
url=".../dynctls.aspx" />
<siteMapNode title="ViewState"
url=".../viewstate.aspx" />
</siteMapNode>
<siteMapNode title="Chapter 4" />
</siteMapNode>
<siteMapNode title="Part 2">
<siteMapNode title="Chapter 9">
<siteMapNode title="Site map"
url=".../sitemapinfo.aspx" />
</siteMapNode>
</siteMapNode>
<siteMapNode title="Part 3"
url="samples.aspx?partid=3" />
</siteMapNode>
</siteMapNode>
</siteMap>
Note that the url attribute is optional. If the attribute is not defined, the node is intended to be an inert container and won’t be made clickable.
Note