<p>I've got a DataTable containing a sitemap hierarchy with the following columns:</p>

<ul>
<li>ItemId</li>
<li>ParentId</li>
<li>Name</li>
<li>Url</li>
</ul>

<p>I need to generate a set of nested lists in HTML (left the anchor elements out for clarity):</p>

<pre><code>&lt;ul&gt;
&lt;li&gt;Item 1&lt;/li&gt;
&lt;li&gt;Item 2&lt;/li&gt;
    &lt;ul&gt;
    &lt;li&gt;Sub Item 1&lt;/li&gt;
    &lt;li class="current"&gt;Sub Item 2&lt;/li&gt;
    &lt;/ul&gt;
&lt;li&gt;Item 3&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>The tree should only contain branches that lead to the 'current' node/page (so using the above example any child items that Item '1' or '3' have are not displayed. Can anyone help out with some pseudo code / code example that can traverse the tree from the leaf to root building the HTML as it goes? Thanks.</p>

I made something similar, it might not be that efficient but its easy to debug.
Find path to selected node.
Add rootnodes to list.
Find node in list which is in path.
Add all childern to this node.
Find next node in path.
Repeat 4-5 until at selected node, and if not at leaf add selected nodes childern as well.
Alternative:
1. Find selected node, print this and all siblings
2. Print parent and all siblings around the string from 1.
3. Repeat 2 until at root.