Kit Sunde Kit Sunde - 6 months ago 4x Question

How can I turn the output of asp:SiteMapPath into a list?

I'm extremely unfamiliar with both .NET and VB.NET and can't quite figure out how to do this. Say I have code like this:

<div class="breadcrumb">
<asp:SiteMapPath ID="SiteMapPath1" runat="server"></asp:SiteMapPath>

It outputs a bunch of
s with
as separators, something like this:

<div class="breadcrumb">
<span id="ctl00_SiteMapPath1">
<a href="#ctl00_SiteMapPath1_SkipLink">
<img alt="Skip Navigation Links" height="0" width="0" src="/Bonfield/WebResource.axd?d=PEpmmIw6qvhaEC3hEwXGjgvJKlzc3DOMu_e-zW-n6pfl6YR-iYjwmlvrYPb689EslKxysA7aoh_x_ALjLs5QXiz7NG41&amp;t=634245478914809245" style="border-width:0px;" />
<a href="/Bonfield/Default.aspx">Home</a>
<span> &#187; </span>
<span>Showcase</span><a id="ctl00_SiteMapPath1_SkipLink"></a></span>

How can I turn that into a list like:



You may have solved this by now but you could use this function to loop through all the items in the rootnode of a sitemap and their descendants and build up a nested list.

You can remove If item.HasChildNodes Then sb.Append(ListChildNodes(item)) if you are only interested in the top level

 Public Function SiteMap() As String
        Return ListChildNodes(System.Web.SiteMap.RootNode)
    End Function

    Private Function ListChildNodes(ByVal node As System.Web.SiteMapNode) As String
        Dim sb As New System.Text.StringBuilder

        For Each item As SiteMapNode In node.ChildNodes
            sb.Append(String.Concat("<li><a href=""", item.Url, """>", item.Title, "</a></li>"))
            If item.HasChildNodes Then sb.Append(ListChildNodes(item))

        Return sb.ToString
    End Function

For those who would like the C# version:

public string SiteMap()
            return ListChildNodes(System.Web.SiteMap.RootNode);
        private string ListChildNodes(System.Web.SiteMapNode node)
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (SiteMapNode item in node.ChildNodes)
                sb.Append(string.Concat("<li><a href=\"", item.Url, "\">", item.Title, "</a></li>"));
                if (item.HasChildNodes)

            return sb.ToString();

Then in your code you can just call to output the string to the page.

<h1>Site Map</h1>