Using a bool flag to determine whether in root-level or not
As you see, the method has a third parameter (inRecursion), which indicates whether the method is in recursion, or not. I use it, to remove the leading / in the path.


You need this flag because your function is doing to much. It violates the S (Single responsibility principle) in SOLID. It's not only resolving the elements but it's building the final result at the same time. The method should actually only return a List&lt;string&gt; with all the names (or other itmes) it found so that you can do what ever you need to do with it like:

var names = GetNames(...);
var path = string.Join("/", names);


It doesn't matter if you use recursion or a stack or any other method. The most important part is that it should only do one thing: return a collection of items.

What if you wanted to use a different delimiter such as \ or .? You'd need a new parameter for it. However, if you only have a list of names it's much easier to change it and build a different path because this is another responsibility.