XML Parser with Recursive Function

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'll paste the code and xml from my file below..

Ok here's my question:

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);

}

}
}

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

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

Similar Threads


Top