XML Reading issues

Quick question..

I am hoping to get an answer without posting all the code. What I am running into is that when you run through the loop...it will actually give me double of what I really want.

I think it has to do with the second foreach loop. But how do I fix this?

         //read the document and fill the Connections listview
         foreach (XmlNode xmlNode in xmlDoc.SelectNodes("//MainItem "))
         {
            string sItem1 = xmlNode.Attributes["Item1"].InnerText;
            string sItem2 = xmlNode.Attributes["Item2"].InnerText;
                   
            //read the document and fill the Connections
            foreach (XmlNode xmlNodeType in xmlDoc.SelectNodes("//Connection"))
            {
                  string sType = xmlNodeType.Attributes["Type"].InnerText;
                 
                  if (sType == "newone")
                  {
                  string sAddress = xmlNodeType.FirstChild.Attributes["Address"].InnerText;
                  item.SubItems.Add(sType);
                  item.Tag = sItem1 + "," + sItem2;
                  }
                  else if (sType == "Secondone")
                  {
                  string sAddress = xmlNodeType.FirstChild.Attributes["Address"].InnerText;
                  item.SubItems.Add(sType);
                  item.Tag = sItem1 + "," + sItem2;
                  }                
             }
          }

XML set up like this (example only)
<MainItem Item1 = "bla" item2 = "blabla">
   <Connections  Type ="newone">
      <Parameters Address = "mine" />
   </Connection>
   <Connections  Type ="secondone">
      <Parameters Address = "yours" />
   </Connection>
</MainItem>
<MainItem Item1 = "MYBla" item2 = "MYblabla">
   <Connections  Type ="anothernewone">
      <Parameters Address = "minetoo" />
   </Connection>
   <Connections  Type ="anothersecondone">
      <Parameters Address = "yourstoo" />
   </Connection>
</MainItem>

Right now what I am getting would be a total of 8 different entries instead of a total of 4
LVL 2
NewMom2BrandonAsked:
Who is Participating?
 
GranModCommented:
Closed, 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0
 
mmarksburyCommented:
Change the second foreach to this and give it a try:

foreach (XmlNode xmlNodeType in xmlNode.SelectNodes("//Connection"))
0
 
NewMom2BrandonAuthor Commented:
nope still didn't work
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
NewMom2BrandonAuthor Commented:
I got it to move on. Now I am getting the correct number of childnodes however it is only posting the first one

         //read the document and fill the Connections listview
         foreach (XmlNode xmlNode in xmlDoc.SelectNodes("//MainItem "))
         {
            string sItem1 = xmlNode.Attributes["Item1"].InnerText;
            string sItem2 = xmlNode.Attributes["Item2"].InnerText;
                   
            //read the document and fill the Connections
            if (xmlNodeType.HasChildNodes)
            {
               for (int i=0; i<xmlNodeType.ChildNodes.Count; i++)
               {
                  string sType = xmlNodeType.FirstChild.Attributes["Type"].InnerText;
                 
                  if (sType == "newone")
                  {
                    string sAddress = xmlNodeType.FirstChild.FirstChild.Attributes
                    ["Address"].InnerText;
                    item.SubItems.Add(sType);
                    item.Tag = sItem1 + "," + sItem2;
                  }
                  else if (sType == "Secondone")
                  {
                    string sAddress = xmlNodeType.FirstChild.FirstChild.Attributes
                    ["Address"].InnerText;
                    item.SubItems.Add(sType);
                    item.Tag = sItem1 + "," + sItem2;
                  }  
               }
             }              
           }
         }
0
 
NewMom2BrandonAuthor Commented:
I am getting it to move on by changing this line

string sType = xmlNodeType.FirstChild.Attributes["Type"].InnerText;
 to
string sType = xmlNodeType.ChildNodes[i].Attributes["Type"].InnerText;

but now I am crashing at the line in *********

                  else if (sType == "Secondone")
                  {
                    ***********************
                    string sAddress = xmlNodeType.FirstChild.FirstChild.Attributes
                    ***********************
                    ["Address"].InnerText;
                    item.SubItems.Add(sType);
                    item.Tag = sItem1 + "," + sItem2;
0
 
NewMom2BrandonAuthor Commented:
Got it...

         //read the document and fill the Connections listview
         foreach (XmlNode xmlNode in xmlDoc.SelectNodes("//MainItem "))
         {
            string sItem1 = xmlNode.Attributes["Item1"].InnerText;
            string sItem2 = xmlNode.Attributes["Item2"].InnerText;
                   
            //read the document and fill the Connections
            if (xmlNodeType.HasChildNodes)
            {
               for (int i=0; i<xmlNodeType.ChildNodes.Count; i++)
               {
                  string sType = xmlNodeType.ChildNodes[i].Attributes["Type"].InnerText;
                 
                  if (sType == "newone")
                  {
                    string sAddress = xmlNodeType.ChildNodes[i].FirstChild.Attributes
                    ["Address"].InnerText;
                    item.SubItems.Add(sType);
                    item.Tag = sItem1 + "," + sItem2;
                  }
                  else if (sType == "Secondone")
                  {
                    string sAddress = xmlNodeType.ChildNodes[i].FirstChild.Attributes
                    ["Address"].InnerText;
                    item.SubItems.Add(sType);
                    item.Tag = sItem1 + "," + sItem2;
                  }  
               }
             }              
           }
         }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.