Solved

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

Posted on 2006-06-20
3
258 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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