A while ago, there turned up a question on our.umbraco.org on
how to get hold of all the descendant nodes of a specific document
with a .NET function. View the entire thread
here.
The question was answered, but I'm not really keen on working with
XPath in .NET so I thought I'd propose a recursive approach
instead.
The idea of a recursive function is that you walk up the branches
in the tree, until you reach a leaf. Then return, to see if there
are more branches / leaves on the branch. For example;
We have the structure
-2009
-- 8
--- 19
-- 10
--- 16
--- 22
-- 11
--- 6
In this case, our recursive function start of 2009. When we ask
for Children for 2009 it returns 8,10,11.
We then walk up the 8 and ask for Children. 8 will return 19. We
see then that 19 is a leaf, and therefore adds it to our
list.
Then we return and come back to the 8. Since 8 does not have any
more Children, we also add 8 to our list, and return.
We're back to 2009 and proceed along the next branch which is 10.
10 has two leaves 16 and 22. We add these two and return to 10, and
adds that also.
On the way it is. Or that Sluggo would explain it:

To the programming
We start by adding properties that we know we will need. One for
the start node and one for the document type.
TIP! You know that you can write prop + tab + tab to get
an automatic generation of properties in Visual Studio.
private int rootNode = 0;
public int RootNode { get { return rootNode; } set { rootNode = value; } }
private string documentTypeAlias = "";
public string DocumentTypeAlias { get { return documentTypeAlias; } set { documentTypeAlias = value; } }
Then we go into the Page_Load and declare our list and begin our
recursive function, to which we are submitting our start node, our
list and the document type we want.
List nodeList = new List();
AddAllDescendants(new Node(rootNode), nodeList, documentTypeAlias);
So it's time to create magic, our recursive function.
private void AddAllDescendants(Node CurrentNode, List NodeList, string DocumentTypeAlias) {
foreach (Node n in CurrentNode.Children) {
AddAllDescendants(n, NodeList, DocumentTypeAlias);
}
if (string.IsNullOrEmpty(DocumentTypeAlias) || CurrentNode.NodeTypeAlias == DocumentTypeAlias) {
NodeList.Add(CurrentNode);
}
}
The first thing we do is for all children, call on our recursive
function again. Then we walk up a branch. When we have gone through
the entire foreach loop (or if we reach a leaf that has no foreach)
we will add our node in the list. Here we make a check to see if
node type is the one we are looking for (or if the document is
empty and we will add all the nodes). Subsequently, the function
returns, and then we walks down the tree again.
When we are back in the Page_Load, we have our List of all nodes
of the type we want. From here it is free to do what you like with
your list, eg Sort:
nodeList.Sort(delegate(Node n1, Node n2) {
return n1.Name.CompareTo(n2.Name);
});
Download the full solution here: DescendantNodes_NET.zip