Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-06-20
3
Medium Priority
?
285 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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!
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 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