Solved

URGENT how can I loop through mulitple child nodes..not sure on quantity

Posted on 2006-06-20
3
278 Views
Last Modified: 2010-04-16
Some will have 1 child node and others will have mulitple there is no set limit

here is a example of the section in question

  <Unit ManufactId="8" Id="8"  Connection="2">
    <IT ITId="8" Color="Yellow" />
    <IT ITId="24" Color="Yellow" />
  </Unit>
  <Unit ManufactId="1" Id="1"  Connection="3">
    <IT ITId="1" Color="Black" />
    <IT ITId="2" Color="Black" />
    <IT ITId="3" Color="Black" />
    <IT ITId="4" Color="Black" />
  </Unit>
 
This is what I had in code but it will no longer work.

         //read the document and pull the node into the Listview
         foreach (XmlNode xmlNodeType in xmlDoc.SelectNodes("//Unit"))
         {
            //read the document and fill the Connections listview
            string ManufactId = xmlNodeType.Attributes["ManufactId"].InnerText;
            string Id= xmlNodeType.Attributes["Id"].InnerText;
            string Connection= xmlNodeType.Attributes["Connection"].InnerText;
            string ITId = xmlNodeType.FirstChild.Attributes["ITId "].InnerText;
         
            if (xmlNodeType.HasChildNodes)
            {
               if (xmlNodeType.ChildNodes.Count == 1)
               {
                  string Color = xmlNodeType.FirstChild.Attributes["Color"].InnerText;

                  ListViewItem item = listViewF.Items.Add(ManufactId);
                  item.SubItems.Add(Id);
                  item.SubItems.Add(Connection);
                  item.SubItems.Add(ITId);
                  item.SubItems.Add(Color);
               }
               else
                  if (xmlNodeType.ChildNodes.Count == 2)
               {
                  string Color = xmlNodeType.FirstChild.Attributes["Color"].InnerText;
                  string NextITId = xmlNodeType.FirstChild.NextSibling.Attributes["ITId"].InnerText;
                  string NextColor = xmlNodeType.FirstChild.NextSibling.Attributes["Color"].InnerText;

                  ListViewItem item = listViewF.Items.Add(ManufactId );
                  item.SubItems.Add(Id);
                  item.SubItems.Add(Connection);
                  item.SubItems.Add(ITId);
                  item.SubItems.Add(Color);

                  ListViewItem items = listViewF.Items.Add(ManufactId);
                  items.SubItems.Add(Id);
                  items.SubItems.Add(Connection);
                  items.SubItems.Add(NextITId);
                  items.SubItems.Add(NextColor);
               }
            }
         }
0
Comment
Question by:NewMom2Brandon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Expert Comment

by:e1v
ID: 16942752
I'm not sure I understand your question, but I note one thing: in the line
string ITId = xmlNodeType.FirstChild.Attributes["ITId "].InnerText
you've got a space in the "ITId " that will make the code fail.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 16942784
You should be able to loop through all the children using a second "foreach" loop:

         //read the document and pull the node into the Listview
         foreach (XmlNode xmlNodeType in xmlDoc.SelectNodes("//Unit"))
         {
            //read the document and fill the Connections listview
            string ManufactId = xmlNodeType.Attributes["ManufactId"].InnerText;
            string Id= xmlNodeType.Attributes["Id"].InnerText;
            string Connection= xmlNodeType.Attributes["Connection"].InnerText;
            string ITId = xmlNodeType.FirstChild.Attributes["ITId "].InnerText;

            If (xmlNodeType.HasChildNodes)
            {
                 foreach (XmlNode child in xmlNodeType.ChildNodes)
                 {
                      // etc, etc
                 }
            }
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16942788
How about this:

         //read the document and pull the node into the Listview
         foreach (XmlNode xmlNodeType in xmlDoc.SelectNodes("//Unit"))
         {
            //read the document and fill the Connections listview
            string ManufactId = xmlNodeType.Attributes["ManufactId"].InnerText;
            string Id= xmlNodeType.Attributes["Id"].InnerText;
            string Connection= xmlNodeType.Attributes["Connection"].InnerText;
            foreach (XmlNode xmlNodeIT in xmlNodeType.SelectNodes("IT"))
            {
              string ITId = xmlNodeIT.Attributes["ITId "].InnerText;
              string Color = xmlNodeIT.Attributes["Color"].InnerText;

              ListViewItem item = listViewF.Items.Add(ManufactId );
              item.SubItems.Add(Id);
              item.SubItems.Add(Connection);
              item.SubItems.Add(ITId);
              item.SubItems.Add(Color);

              }
           
         }
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question