Regurgitating a sitemap file as an html list

In my ongoing efforts to create a helper library for myself that consists of commonly used functions in my development efforts, I’ve decided to write a sitemap generator. The concept is simple, I just want the contents of my Web.sitemap file to produce a Sitemap.aspx page which will serve as the basic sitemap for a web site.

I’ve found that on every new web site I’m building in .NET, I need a sitemap for one reason or another. Many times the reason is as simple as making it easy for whomever is writing the content to see what pages still need content and to quickly navigate through the site. It also doesn’t hurt to leave a sitemap link in your sites footer to help out your users and/or search engines.

Requirements

The requirements are simple. Add a .sitemap file to the root of your web site and fill it with the structure of your site. At the minimum you need a valid xml document and a root siteMapNode. Ideally, you would put your entire site structure into the sitemap. For example:

sitemap

Then you can use the following class in your project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SevenLabs.Utilities.Web
{
    public class Helper
    {
        #region Sitemap
        /// <summary>
        /// Builds out the sitemap into html using lists and linking to each page.
        /// </summary>
        /// <param name="rootNode"></param>
        /// <returns>A string holding the html</returns>
        public static string BuildSitemapHtml(SiteMapNode rootNode)
        {
            StringBuilder sb = new StringBuilder();
            if (rootNode != null)
            {
                sb.AppendFormat("<h1><a href='{0}' runat='server'>{1}</a></h1>", rootNode.Url, rootNode.Title);

                if (rootNode.HasChildNodes)
                {
                    sb.Append("<ul>");
                    sb = BuildSitemapChildHtml(rootNode, sb);
                    sb.Append("</ul>");
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// Recursively build childNodes for a sitemap
        /// </summary>
        /// <param name="rootNode"></param>
        /// <param name="sb"></param>
        /// <returns></returns>
        private static StringBuilder BuildSitemapChildHtml(SiteMapNode rootNode, StringBuilder sb)
        {
            foreach (SiteMapNode node in rootNode.ChildNodes)
            {
                if (node.HasChildNodes)
                {
                    sb.AppendFormat("<h3><a href='{0}' runat='server'>{1}</a></h3>", node.Url, node.Title);
                    sb.Append("<ol>");
                    foreach (SiteMapNode child in node.ChildNodes)
                    {
                        if (child.HasChildNodes)
                        {
                            sb.AppendFormat("<h3><a href='{0}' runat='server'>{1}</a></h3>", child.Url, child.Title);
                            sb = BuildSitemapChildHtml(child, sb);
                        }

                        sb.AppendFormat("<li><a href='{0}' runat='server'>{1}</a></li>", child.Url, child.Title);
                    }
                    sb.Append("</ol>");
                }
                else if (rootNode == SiteMap.RootNode)
                {
                    sb.AppendFormat("<h3><a href='{0}' runat='server'>{1}</a></h3>", node.Url, node.Title);
                }
                else
                {
                    sb.AppendFormat("<li><a href='{0}' runat='server'>{1}</a></li>", node.Url, node.Title);
                }
            }
            return sb;
        }
        #endregion
    }
}

Building the Sitemap.aspx page

Now that you have your sitemap and the helper class, building the Sitemap.aspx page is extremely simple. Calling the following from a normal .aspx page will create a tree structure containing links to all of the pages in your site:

<div id="Sitemap">
<%=SevenLabs.Utilities.Web.Helper.BuildSitemapHtml(SiteMap.RootNode) %>
</div>

The surrounding DIV is just so you can style the elements to your liking.

This is the resulting .aspx page:

sitemap_page

Helper.cs (2.61 kb)

kick it on DotNetKicks.com


Related posts

Comments

November 26. 2008 09:54 PM

Busby SEO Test

thanks i really enjoy reading your article!!

Busby SEO Test

December 16. 2008 08:30 PM

Busby SEO TEST

Sorry i am not a programmer, but does the sitemap file above follow google sitemap?

Busby SEO TEST

January 1. 2009 11:36 PM

Busby SEO Test

great sitemap generator

Busby SEO Test

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 7. 2009 04:20 AM

Search

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009