3

I've read related questions on this but I feel I'm not grokking it. If I have an XML document with many nodes and subsequent child nodes, how do I loop through the document if I don't know how deep the child nodes go?

Here is some horrible code to demo what I mean:

foreach (var section in xml.Sections.Keys)
{
    cont.ContentControls.Add(new Separator(xml.Sections[section].Name));
    foreach (var variable in xml.Sections[section].Variables)
    {
        TraverseVars(cont, xml.Sections[section].Name, variable.Value.Name, variable.Value.Title, variable.Value.Default1, variable.Value.Default2, variable.Value.Default3, variable.Value.DesignerType);
        i++;
    }
    if (xml.Sections[section].Sections.Count > 0)
    {
        foreach (var section2 in xml.Sections[section].Sections.Keys)
        {
            cont.ContentControls.Add(new Separator(xml.Sections[section].Sections[section2].Name));
            foreach (var variable2 in xml.Sections[section].Sections[section2].Variables)
            {
                TraverseVars(cont, xml.Sections[section].Name, variable2.Value.Name, variable2.Value.Title, variable2.Value.Default1, 
                            variable2.Value.Default2, variable2.Value.Default3, variable2.Value.DesignerType);
                i++;
            }
        }
    }
}

Here I've only catered for when there is one nested level ("if......Count > 0"). I know there is a better way to cater for nested levels but I can't see it.

3
  • 1
    If you are looking to select a particular node no matter the level it's at you can use a select with "//nodename" which grabs all nodes with that name. Commented Feb 21, 2011 at 12:33
  • Check following link on MSDN Forums; Iterate through all nodes in an XML document social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/… Commented Feb 21, 2011 at 12:34
  • dont forgot to mark the answer the one which helps you... Commented Feb 21, 2011 at 14:59

2 Answers 2

3

For this you have to create one recursive function, that will call it self untill the nested xml loop are not over ...

here is an example...

PLease fix if any mistake is ther...

public void xmlsection(XmlSection Section)
{
    cont.ContentControls.Add(new Separator(xml.Sections[section].Name));
    foreach (var variable in xml.Sections[section].Variables)
    {
        TraverseVars(cont, xml.Sections[section].Name, variable.Value.Name, variable.Value.Title, variable.Value.Default1, variable.Value.Default2, variable.Value.Default3, variable.Value.DesignerType);
        i++;
    }
    if (xml.Sections[section].Sections.Count > 0)
    {
        foreach (var section2 in xml.Sections[section].Sections.Keys)
        {
xmlsection(section2);
        }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

The i++ won't work anymore probably, but I'm sure the OP will figure out a way to resolve that issue. ;p
@steven yes you are right i have not just written the whole code. I have just shown the way to do recursive function.
3

What you are looking for is either recursion, or a while loop where you reassign a variable which you use inside your loop constantly.


Using recursion is probably easiest, take a look at Bhavik Goyal answer, it seems to be your solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.