Recursive Function Question

J

John Underwood

Hi.. I was hoping someone could help me understand whats happening
in my code, it works fine, I just don't understand one part of it
and would like to.. I've pasted some c# code and xml below..

When I open the xml file, it calls myTest and passes in the xmlNode
and is recursive in calling itself.. I can get all the data just
fine. What I don't understand is when it reaches the line:

if (inXmlNode.HasChildNodes)

and that node has no children so it's false, it of course skips
the code in braces.. If i'm stepping through the code I notice at this point it
goes to the closing brace for the myTest function, then when i step
again, it goes to the following line.

for(int i = 0; i<=nodeList.Count - 1; i++)

At this point the node has stepped back one.. For example: In my xml
code the HasChildNodes would be false when the node was equal to <mytest>,
but after stepping to the bottom brace(after the hasChildNodes was false),
and then stepping again, it jumps
back to the for statement I mentioned above, and the node (inXmlNode)
is now the previous node <new> .. Now it increments i in the for
statement and fails, so it goes to the bottom brace again, then I step
one more time, it goes back to the For statement, and now the node (inXmlNode)
is equal to <xmltest> the previous node.. It now moves on to the next
node which is <view> in this example. So it works fine, but I don't get
how the Node is moving back a step, from the point of the bottom brace, to the
previous step, which puts it back at the for statement again.

It moves from <mytest> to <new> back to <xmltest> in the scenario I just
explained, and i'm not understand how...


I really hope I explained this well, I'm pretty new to this stuff, sorry
for so long, was not sure how to better explain it.. any help would
be great appreciated..

Thanks,
John Underwood



-----------------------------------------------------------------
XML I'm reading in............

<?xml version="1.0"?>
<xmltest>
<new value="someValue" label="someLabel">
<mytest value="someValue" label="someLable"/>
</new>
<view value="someValue" label="someLable"/>
</xmltest>
-----------------------------------------------------------------
private void button2_Click(object sender, System.EventArgs e)
{
XmlDocument dom = new XmlDocument();
XmlTextReader reader = new XmlTextReader(@"\XMLFile1.xml");
dom.Load(reader);
myTest(dom.DocumentElement);
}
private void myTest(XmlNode inXmlNode)
{
XmlNode xNode;
XmlNodeList nodeList;

if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;

for(int i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes;
myTest(xNode);
<snip>

}

}
}

-----------------------------------------------------------------
 
A

Adam W Root

What's happening is you are seeing the debugger step into the function once,
then again, etc, then after it completes one of the function calls (you see
it jump to the closing brace) it continues execution of the PREVIOUS
function call, but since it was the same function, it appears to jump
backwards. If you watch a stack trace, you will see the number of levels
deep in your recursion grow and shrink as it walks the tree of XML.

Hope this explains it a bit better. Recursion is a very cool feature once
you understand it. Try stepping through the code with pen and a print out of
the xml so you understand it. There may be a better way to explain it
quickly than what I just offered, but pictures would probably help a great
deal. (You may even find some simple examples on the net).

Good luck

Adam

John Underwood said:
Hi.. I was hoping someone could help me understand whats happening
in my code, it works fine, I just don't understand one part of it
and would like to.. I've pasted some c# code and xml below..

When I open the xml file, it calls myTest and passes in the xmlNode
and is recursive in calling itself.. I can get all the data just
fine. What I don't understand is when it reaches the line:

if (inXmlNode.HasChildNodes)

and that node has no children so it's false, it of course skips
the code in braces.. If i'm stepping through the code I notice at this point it
goes to the closing brace for the myTest function, then when i step
again, it goes to the following line.

for(int i = 0; i<=nodeList.Count - 1; i++)

At this point the node has stepped back one.. For example: In my xml
code the HasChildNodes would be false when the node was equal to <mytest>,
but after stepping to the bottom brace(after the hasChildNodes was false),
and then stepping again, it jumps
back to the for statement I mentioned above, and the node (inXmlNode)
is now the previous node <new> .. Now it increments i in the for
statement and fails, so it goes to the bottom brace again, then I step
one more time, it goes back to the For statement, and now the node (inXmlNode)
is equal to <xmltest> the previous node.. It now moves on to the next
node which is <view> in this example. So it works fine, but I don't get
how the Node is moving back a step, from the point of the bottom brace, to the
previous step, which puts it back at the for statement again.

It moves from <mytest> to <new> back to <xmltest> in the scenario I just
explained, and i'm not understand how...


I really hope I explained this well, I'm pretty new to this stuff, sorry
for so long, was not sure how to better explain it.. any help would
be great appreciated..

Thanks,
John Underwood



-----------------------------------------------------------------
XML I'm reading in............

<?xml version="1.0"?>
<xmltest>
<new value="someValue" label="someLabel">
<mytest value="someValue" label="someLable"/>
</new>
<view value="someValue" label="someLable"/>
</xmltest>
-----------------------------------------------------------------
private void button2_Click(object sender, System.EventArgs e)
{
XmlDocument dom = new XmlDocument();
XmlTextReader reader = new XmlTextReader(@"\XMLFile1.xml");
dom.Load(reader);
myTest(dom.DocumentElement);
}
private void myTest(XmlNode inXmlNode)
{
XmlNode xNode;
XmlNodeList nodeList;

if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;

for(int i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes;
myTest(xNode);
<snip>

}

}
}

-----------------------------------------------------------------
 
S

Steve Long

I'll try an offer another explaination that may be helpful.

For every recursive call in your function, execution of the current function
halts and steps into the new function call. This behavior continues until
your code reaches the end condition that allows the recursion to unwind.
Once it begins to unwind, the code in the function that was not reached
previously, now executes and continues until the recursion completely
unwinds.

HTH
Steve


John Underwood said:
Hi.. I was hoping someone could help me understand whats happening
in my code, it works fine, I just don't understand one part of it
and would like to.. I've pasted some c# code and xml below..

When I open the xml file, it calls myTest and passes in the xmlNode
and is recursive in calling itself.. I can get all the data just
fine. What I don't understand is when it reaches the line:

if (inXmlNode.HasChildNodes)

and that node has no children so it's false, it of course skips
the code in braces.. If i'm stepping through the code I notice at this point it
goes to the closing brace for the myTest function, then when i step
again, it goes to the following line.

for(int i = 0; i<=nodeList.Count - 1; i++)

At this point the node has stepped back one.. For example: In my xml
code the HasChildNodes would be false when the node was equal to <mytest>,
but after stepping to the bottom brace(after the hasChildNodes was false),
and then stepping again, it jumps
back to the for statement I mentioned above, and the node (inXmlNode)
is now the previous node <new> .. Now it increments i in the for
statement and fails, so it goes to the bottom brace again, then I step
one more time, it goes back to the For statement, and now the node (inXmlNode)
is equal to <xmltest> the previous node.. It now moves on to the next
node which is <view> in this example. So it works fine, but I don't get
how the Node is moving back a step, from the point of the bottom brace, to the
previous step, which puts it back at the for statement again.

It moves from <mytest> to <new> back to <xmltest> in the scenario I just
explained, and i'm not understand how...


I really hope I explained this well, I'm pretty new to this stuff, sorry
for so long, was not sure how to better explain it.. any help would
be great appreciated..

Thanks,
John Underwood



-----------------------------------------------------------------
XML I'm reading in............

<?xml version="1.0"?>
<xmltest>
<new value="someValue" label="someLabel">
<mytest value="someValue" label="someLable"/>
</new>
<view value="someValue" label="someLable"/>
</xmltest>
-----------------------------------------------------------------
private void button2_Click(object sender, System.EventArgs e)
{
XmlDocument dom = new XmlDocument();
XmlTextReader reader = new XmlTextReader(@"\XMLFile1.xml");
dom.Load(reader);
myTest(dom.DocumentElement);
}
private void myTest(XmlNode inXmlNode)
{
XmlNode xNode;
XmlNodeList nodeList;

if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;

for(int i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes;
myTest(xNode);
<snip>

}

}
}

-----------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top