Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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

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
NewMom2Brandon
Asked:
NewMom2Brandon
1 Solution
 
e1vCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
dstanley9Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now