Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now