troubleshooting Question

Reading XML parent and children

Avatar of Starr Duskk
Starr DuskkFlag for United States of America asked on
XMLASP.NET
7 Comments1 Solution264 ViewsLast Modified:
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> 

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

Thanks!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros