Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

Reading XML parent and children

I have parent and child nodes as below. I want to retrieve the parent description, then traverse and read the child names.

So I could possibly have the below printout when done with 3 parents, with 1-3 children each as shown below (there can be X children):
Description1 - Child Name1
Description1 - Child Name2
Description2 - Child Name1
Description3 - Child Name1
Description3 - Child Name2
Description3 - Child Name3

                <parent>
                  <description>Test1</description>
                  <children>
                    <child>
                      <name>Joe</name>
                    </child>
                    <child>
                      <name>Jane</name>
                    </child>
                  </children>
                </parent> 
                <parent>
                  <description>Test2</description>
                  <children>
                    <child>
                      <name>Joe2</name>
                    </child>
                    <child>
                      <name>Jane2</name>
                    </child>
                  </children>
                </parent> 

Open in new window


I'm starting out with something like below, but once I hit the child within the child node (children/child) I don't know how to access and loop through all the children:
 XmlDocument xmlResults = new XmlDocument();
xmlResults.LoadXml(xmlString);

XmlNodeList nodes = xmlResults.GetElementsByTagName("parent");
if (nodes != null)
 {

       foreach (XmlNode node in nodes)
       {
             string description = node["description"].InnerText;
             string name = node["name"].InnerText;
       }
}

Open in new window


Thanks!
Avatar of Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano
Flag of United States of America image

Replace lines 8-12 with code below:
       foreach (XmlNode node in nodes)
       {
             string description = node["description"].InnerText;
             if (!node.HasChildNodes)
             {
                 //Print description alone
                 continue;
              }
             XmlNodeList childNodes = xmlResults.GetElementsByTagName("child");
             foreach (XmlNode childNode in childNodes)
             {
                 string name = childNode["name"].InnerText;
                 //Print description and names
              }
       }

Note: You can use xpath to get the child node if you like
Avatar of Starr Duskk

ASKER

mas,

I tried using xpath, and couldn't get it to ever work.

How would I do an xpath with this structure?

thanks.
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>XmlNodeList childNodes = node.GetElementsByTagName("child");

Just fyi, it says on this line that GetElementsByTagName doesn't have a definition in System.Xml.XmlNode

I'm going to try the Xpath now.

Thanks!
It kind of works.

But it is returning me every single child in the entire file with every parent.

Like:
parent:Test
Joe
Jane
Joe2
Jane2

parent:Test2
Joe
Jane
Joe2
Jane2

Joe and Jane belong with the first one and Joe2 and Jane2 belong with the second one, but on every loop it returns all children in the original xml, not just in the selected node.

Weird.

Any ideas?

thanks.
Oh well. Guess I'll close this. Going a different route.