First, the difficulty may be here:
var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, ObjectImageIndex, ObjectImageIndex);
inTreeNode.Nodes.Add(tNode2);
tNode2 = inTreeNode.Nodes[x]; ?
AddNode(xNode, tNode2);
You create a new TreeNode, add it to the collection, and then re-assign it to the current "slot" pointed to by the indexed value
in the same collection. Why don't you just pass 'tNode2 directly to the call to 'AddNode ?
And, possibly, add some code that checks that the 'Count properties of the 'inXmlNode collection and the 'inTreeNode collection are valid for all possible values of 'x.
var inXmlNodes = inXmlNode.ChildNodes;
int xmlNodeCount = inXmlNodes.Count();
var inTreeNodes = inTreeNode.Nodes;
if(inTreeNode.Count < xmlNodeCount {
If that doesn't help you, then put a break-point just before the call to 'AddNode and follow the execution path when that break-point is reached.