• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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
0
NewMom2Brandon
Asked:
NewMom2Brandon
  • 4
1 Solution
 
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
 
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
Industry Leaders: 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!

 
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
 
GranModCommented:
Closed, 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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