Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Looping through XMLNodeList problem

Looping through XMLNodeList problem

Scheduled Pinned Locked Moved C#
debuggingxmlhelp
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jamesc69
    wrote on last edited by
    #1

    Hi, I have XML in the following structure:    <Rooms>       <Room>          <RoomRQId>1</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>3</NumAdults>       </Room>       <Room>          <RoomRQId>2</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>2</NumAdults>       </Room>    </Rooms> And each time, I get the values assigned as the first occurence of room.   Running in debug mode, I can see the second node called within the For each loop, but the values are still assigned as the second occurence.   Code below: XmlNodeList nodeRooms = xmlDoc.SelectNodes("/test:HotelAvail/test:Rooms/test:Room", ns); foreach (XmlNode objRNode in nodeRooms)                      {                            intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText);                                 strQuantity = objRNode["Quantity"].InnerText;                            intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); }                So IntRoomRQID is set as 1 twice, when it should create a node of 1, and then a node of value 2.

    K realJSOPR 2 Replies Last reply
    0
    • J jamesc69

      Hi, I have XML in the following structure:    <Rooms>       <Room>          <RoomRQId>1</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>3</NumAdults>       </Room>       <Room>          <RoomRQId>2</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>2</NumAdults>       </Room>    </Rooms> And each time, I get the values assigned as the first occurence of room.   Running in debug mode, I can see the second node called within the For each loop, but the values are still assigned as the second occurence.   Code below: XmlNodeList nodeRooms = xmlDoc.SelectNodes("/test:HotelAvail/test:Rooms/test:Room", ns); foreach (XmlNode objRNode in nodeRooms)                      {                            intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText);                                 strQuantity = objRNode["Quantity"].InnerText;                            intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); }                So IntRoomRQID is set as 1 twice, when it should create a node of 1, and then a node of value 2.

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      The poblem is this bit of code:

      jamesc69 wrote:

      foreach (XmlNode objRNode in nodeRooms) { intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); strQuantity = objRNode["Quantity"].InnerText; intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); }

      It will set the 3 variables (intRoomRQId, strQuantity intNumAdults), move to the next node then, before you have done anything with them, re-set with the values from the next node. The code posted will always end up with the variables set to the value of the last node. To fix this you either need to do whatever it is you need to do with the variables in the loop, or create an object to store the three variables, and maintain a List of these which can be iterated over later. As a side note intRoomRQId, strQuantity and intNumAdults are poorly named, you shouldn't include the type in the variable identifier in C#, and fully named is better. This will improve the readability of your code. I suggest roomRequestId (or whatever RQ means), quantityand numberOfAdults

      CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

      J 1 Reply Last reply
      0
      • K Keith Barrow

        The poblem is this bit of code:

        jamesc69 wrote:

        foreach (XmlNode objRNode in nodeRooms) { intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); strQuantity = objRNode["Quantity"].InnerText; intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); }

        It will set the 3 variables (intRoomRQId, strQuantity intNumAdults), move to the next node then, before you have done anything with them, re-set with the values from the next node. The code posted will always end up with the variables set to the value of the last node. To fix this you either need to do whatever it is you need to do with the variables in the loop, or create an object to store the three variables, and maintain a List of these which can be iterated over later. As a side note intRoomRQId, strQuantity and intNumAdults are poorly named, you shouldn't include the type in the variable identifier in C#, and fully named is better. This will improve the readability of your code. I suggest roomRequestId (or whatever RQ means), quantityand numberOfAdults

        CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

        J Offline
        J Offline
        jamesc69
        wrote on last edited by
        #3

        Hi, this is merely a snippet. The variables are used within the loop, (but I didn't include all that). In debug, each variable is set to the first node value, and then when it loops through again, it sets it again to the first node value. Therefore it is looping the correct number of times, the problem seems to be in identifying the xpath to then set the variable to each occurence.

        K 1 Reply Last reply
        0
        • J jamesc69

          Hi, this is merely a snippet. The variables are used within the loop, (but I didn't include all that). In debug, each variable is set to the first node value, and then when it loops through again, it sets it again to the first node value. Therefore it is looping the correct number of times, the problem seems to be in identifying the xpath to then set the variable to each occurence.

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          Can you post fuller code? Also is the code being called the correct number of times (ie twice) even if the code is only setting from one node? [Edit] I ran this scratch code:

                  XmlDocument xmlDoc = new XmlDocument();
                  xmlDoc.LoadXml("<HotelAvail><Rooms><Room><RoomRQId>1</RoomRQId><Quantity>1</Quantity><NumAdults>3</NumAdults></Room><Room><RoomRQId>2</RoomRQId><Quantity>1</Quantity><NumAdults>2</NumAdults></Room></Rooms></HotelAvail>");
                  
                  XmlNodeList nodeRooms = xmlDoc.SelectNodes("HotelAvail/Rooms/Room");
                  foreach (XmlNode objRNode in nodeRooms)
                  {
                      Console.Write(objRNode\["RoomRQId"\].InnerText);
                      Console.Write("\\t");
                      Console.Write(objRNode\["Quantity"\].InnerText);
                      Console.Write("\\t");
                      Console.WriteLine(objRNode\["NumAdults"\].InnerText);
                  }
          
                  Console.ReadKey();
          

          This code outputs as expected: 1    1    3 2    1    2 So it is something else inside the loop causing the problem

          CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

          modified on Monday, January 11, 2010 6:20 AM

          J 1 Reply Last reply
          0
          • K Keith Barrow

            Can you post fuller code? Also is the code being called the correct number of times (ie twice) even if the code is only setting from one node? [Edit] I ran this scratch code:

                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml("<HotelAvail><Rooms><Room><RoomRQId>1</RoomRQId><Quantity>1</Quantity><NumAdults>3</NumAdults></Room><Room><RoomRQId>2</RoomRQId><Quantity>1</Quantity><NumAdults>2</NumAdults></Room></Rooms></HotelAvail>");
                    
                    XmlNodeList nodeRooms = xmlDoc.SelectNodes("HotelAvail/Rooms/Room");
                    foreach (XmlNode objRNode in nodeRooms)
                    {
                        Console.Write(objRNode\["RoomRQId"\].InnerText);
                        Console.Write("\\t");
                        Console.Write(objRNode\["Quantity"\].InnerText);
                        Console.Write("\\t");
                        Console.WriteLine(objRNode\["NumAdults"\].InnerText);
                    }
            
                    Console.ReadKey();
            

            This code outputs as expected: 1    1    3 2    1    2 So it is something else inside the loop causing the problem

            CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

            modified on Monday, January 11, 2010 6:20 AM

            J Offline
            J Offline
            jamesc69
            wrote on last edited by
            #5

            The entire project is huge and references several classes, so it's impossible to include the whole code, below is a more concise example of what is occuring.   Basically for every occurence of the "Room" node, I populate a datatable with the variables, and then write an output.   It loops through my for each loop twice, but sets the variables each time to the first occurence only.   And therefore I get two nodes output, which are both derived from the first datatable created, and the first set of room child nodes in the xml document.   The rest of this project is working 100% perfectly, but this is the only area where I need to loop through a list of nodes, as all other nodes occur once. XmlNodeList nodeRooms = xmlDoc.SelectNodes("/test:HotelAvail/test:Rooms/test:Room", ns); foreach (XmlNode objRNode in nodeRooms) { intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); strQuantity = objRNode["Quantity"].InnerText; intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); DataTable dt = objAvailability.SelectHotelAvail(intRoomRQId, strQuantity, intNumAdults); if (dt.Rows.Count >0) {    objWriter.WriteStartElement("Room");    objWriter.WriteStartElement("RoomRQId");    objWriter.WriteString(intRoomRQId.ToString());    objWriter.WriteEndElement(); //RoomRQID    ..... other items from datatable are written here } }

            L K 2 Replies Last reply
            0
            • J jamesc69

              The entire project is huge and references several classes, so it's impossible to include the whole code, below is a more concise example of what is occuring.   Basically for every occurence of the "Room" node, I populate a datatable with the variables, and then write an output.   It loops through my for each loop twice, but sets the variables each time to the first occurence only.   And therefore I get two nodes output, which are both derived from the first datatable created, and the first set of room child nodes in the xml document.   The rest of this project is working 100% perfectly, but this is the only area where I need to loop through a list of nodes, as all other nodes occur once. XmlNodeList nodeRooms = xmlDoc.SelectNodes("/test:HotelAvail/test:Rooms/test:Room", ns); foreach (XmlNode objRNode in nodeRooms) { intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); strQuantity = objRNode["Quantity"].InnerText; intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); DataTable dt = objAvailability.SelectHotelAvail(intRoomRQId, strQuantity, intNumAdults); if (dt.Rows.Count >0) {    objWriter.WriteStartElement("Room");    objWriter.WriteStartElement("RoomRQId");    objWriter.WriteString(intRoomRQId.ToString());    objWriter.WriteEndElement(); //RoomRQID    ..... other items from datatable are written here } }

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Seems to me intRoomRQId is updating as it should, and the bug is inside SelectHotelAvail(), returning the same row over and over for unknown reasons. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Happy New Year to all.
              We hope 2010 soon brings us automatic PRE tags!
              Until then, please insert them manually.


              1 Reply Last reply
              0
              • J jamesc69

                The entire project is huge and references several classes, so it's impossible to include the whole code, below is a more concise example of what is occuring.   Basically for every occurence of the "Room" node, I populate a datatable with the variables, and then write an output.   It loops through my for each loop twice, but sets the variables each time to the first occurence only.   And therefore I get two nodes output, which are both derived from the first datatable created, and the first set of room child nodes in the xml document.   The rest of this project is working 100% perfectly, but this is the only area where I need to loop through a list of nodes, as all other nodes occur once. XmlNodeList nodeRooms = xmlDoc.SelectNodes("/test:HotelAvail/test:Rooms/test:Room", ns); foreach (XmlNode objRNode in nodeRooms) { intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); strQuantity = objRNode["Quantity"].InnerText; intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); DataTable dt = objAvailability.SelectHotelAvail(intRoomRQId, strQuantity, intNumAdults); if (dt.Rows.Count >0) {    objWriter.WriteStartElement("Room");    objWriter.WriteStartElement("RoomRQId");    objWriter.WriteString(intRoomRQId.ToString());    objWriter.WriteEndElement(); //RoomRQID    ..... other items from datatable are written here } }

                K Offline
                K Offline
                Keith Barrow
                wrote on last edited by
                #7

                I edited my last post with some scratch code based on yours, it is iterating through correctly. The things that are different (on mine) are: 1. I don't have any namespaces. 2. I had to modify the XML you posted to have a root element The code you have posted just looks like it should work OK to me. A few things to check: a. I assume you've done this alread, but put a breakpoint at the start of the foreach loop and inspected the nodeRooms list manually, and checked the objNode object at each iteration. b. I assume intRoomRQId, strQuantity, intNumAdults value types (string, int etc) otherwise there is a danger objAvailability.SelectHotelAvail(intRoomRQId, strQuantity, intNumAdults); will change them. c. Do you have more than one Rooms node? Also, how can you tell that only the first set of values are being used (e.g. from, the database table, by breakpoint), related to this, what is objWriter? It is possible that the writer is not wroking correctly.

                CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                J 1 Reply Last reply
                0
                • K Keith Barrow

                  I edited my last post with some scratch code based on yours, it is iterating through correctly. The things that are different (on mine) are: 1. I don't have any namespaces. 2. I had to modify the XML you posted to have a root element The code you have posted just looks like it should work OK to me. A few things to check: a. I assume you've done this alread, but put a breakpoint at the start of the foreach loop and inspected the nodeRooms list manually, and checked the objNode object at each iteration. b. I assume intRoomRQId, strQuantity, intNumAdults value types (string, int etc) otherwise there is a danger objAvailability.SelectHotelAvail(intRoomRQId, strQuantity, intNumAdults); will change them. c. Do you have more than one Rooms node? Also, how can you tell that only the first set of values are being used (e.g. from, the database table, by breakpoint), related to this, what is objWriter? It is possible that the writer is not wroking correctly.

                  CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                  J Offline
                  J Offline
                  jamesc69
                  wrote on last edited by
                  #8

                  Here is a complete XML snapshot: <?xml version="1.0" encoding="utf-8">; <HotelAvail xmlns="http://www.transhotel.com/transHotel/2004A" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.transhotel.com/transHotel/2004A HotelAvailRQ.xsd">    <WholesalerID>TRANSHOTEL</WholesalerID>    <Hotels>       <HotelCode>1154718</HotelCode>    </Hotels>    <Dates>       <CheckIn>2010-01-21</CheckIn>       <CheckOut>2010-01-22</CheckOut>    </Dates>    <Rooms>       <Room>          <RoomRQId>1</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>3</NumAdults>       </Room>       <Room>          <RoomRQId>2</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>2</NumAdults>       </Room>    </Rooms> </HotelAvail> If I run in debug mode, on the first pass through ForEach it says that the innertext will be 113 (for the whole occurrence of that node), on the second pass it says that the innertext will be 212 (which is correct).   So the problem has got to be how the variables are set from the referenced nodes.   For some reason it is selecting the 1st occurence of each element within Room even though it is running within a loop.   I know this can be a problem with say SelectSingleNode, but I'm not using that.   So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document. A namespace is used and referenced as follows: XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);                   ns.AddNamespace("test", "http://www.transhotel.com/transHotel/2004A"); There is only one "Rooms" node, so that is selected explicitly so as to ensure no confusion.   Objwriter is an XMLTextWriter which is merely writing the output.

                  K 1 Reply Last reply
                  0
                  • J jamesc69

                    Here is a complete XML snapshot: <?xml version="1.0" encoding="utf-8">; <HotelAvail xmlns="http://www.transhotel.com/transHotel/2004A" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.transhotel.com/transHotel/2004A HotelAvailRQ.xsd">    <WholesalerID>TRANSHOTEL</WholesalerID>    <Hotels>       <HotelCode>1154718</HotelCode>    </Hotels>    <Dates>       <CheckIn>2010-01-21</CheckIn>       <CheckOut>2010-01-22</CheckOut>    </Dates>    <Rooms>       <Room>          <RoomRQId>1</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>3</NumAdults>       </Room>       <Room>          <RoomRQId>2</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>2</NumAdults>       </Room>    </Rooms> </HotelAvail> If I run in debug mode, on the first pass through ForEach it says that the innertext will be 113 (for the whole occurrence of that node), on the second pass it says that the innertext will be 212 (which is correct).   So the problem has got to be how the variables are set from the referenced nodes.   For some reason it is selecting the 1st occurence of each element within Room even though it is running within a loop.   I know this can be a problem with say SelectSingleNode, but I'm not using that.   So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document. A namespace is used and referenced as follows: XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);                   ns.AddNamespace("test", "http://www.transhotel.com/transHotel/2004A"); There is only one "Rooms" node, so that is selected explicitly so as to ensure no confusion.   Objwriter is an XMLTextWriter which is merely writing the output.

                    K Offline
                    K Offline
                    Keith Barrow
                    wrote on last edited by
                    #9

                    jamesc69 wrote:

                    If I run in debug mode, on the first pass through ForEach it says that the innertext will be 113 (for the whole occurrence of that node), on the second pass it says that the innertext will be 212 (which is correct).

                    OK, so we've eliminated two possibilities: The xmlDoc.SelectNodes is selecting the node correctly and the loop is iterating over the nodes correctly.

                    jamesc69 wrote:

                    I know this can be a problem with say SelectSingleNode, but I'm not using that. So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document.

                    Correct _xmlnode_["_NodeName_"].InnerText is not iterative, however you are the source _xmlnode_(in your case objRNode) is the result of an interation. So you will get the objRNode["RoomRQId"].InnerText will get the InnerText of the current objRNode's RoomRQId correctly. Both the code I posted earlier shows that to be the case and Luc Pattyn has tested it.

                    jamesc69 wrote:

                    So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document.

                    Not necessarily (see above). It is possible that, as Luc said, SelectHotelAvail() is fouling up somehow. It is possible that the output code is not working as expected, or that somehow the value is being set in another place (e.g. later in the code, concurrency problems). Without full source code this is hard to find. The line intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); should get the correct Id. I'd use a breakpoint to ensure that correct ID is being set by that line, then step through to see if it changes.

                    CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                    L J 2 Replies Last reply
                    0
                    • K Keith Barrow

                      jamesc69 wrote:

                      If I run in debug mode, on the first pass through ForEach it says that the innertext will be 113 (for the whole occurrence of that node), on the second pass it says that the innertext will be 212 (which is correct).

                      OK, so we've eliminated two possibilities: The xmlDoc.SelectNodes is selecting the node correctly and the loop is iterating over the nodes correctly.

                      jamesc69 wrote:

                      I know this can be a problem with say SelectSingleNode, but I'm not using that. So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document.

                      Correct _xmlnode_["_NodeName_"].InnerText is not iterative, however you are the source _xmlnode_(in your case objRNode) is the result of an interation. So you will get the objRNode["RoomRQId"].InnerText will get the InnerText of the current objRNode's RoomRQId correctly. Both the code I posted earlier shows that to be the case and Luc Pattyn has tested it.

                      jamesc69 wrote:

                      So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document.

                      Not necessarily (see above). It is possible that, as Luc said, SelectHotelAvail() is fouling up somehow. It is possible that the output code is not working as expected, or that somehow the value is being set in another place (e.g. later in the code, concurrency problems). Without full source code this is hard to find. The line intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); should get the correct Id. I'd use a breakpoint to ensure that correct ID is being set by that line, then step through to see if it changes.

                      CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      keefb wrote:

                      and Luc Pattyn has tested it.

                      No, you misunderstood. I did not test anything, it just seemed from code+observations that the bug was probably inside the method of which no code was shown at all. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      Happy New Year to all.
                      We hope 2010 soon brings us automatic PRE tags!
                      Until then, please insert them manually.


                      K 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        keefb wrote:

                        and Luc Pattyn has tested it.

                        No, you misunderstood. I did not test anything, it just seemed from code+observations that the bug was probably inside the method of which no code was shown at all. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        Happy New Year to all.
                        We hope 2010 soon brings us automatic PRE tags!
                        Until then, please insert them manually.


                        K Offline
                        K Offline
                        Keith Barrow
                        wrote on last edited by
                        #11

                        Apologies, I thought you'd run some code like mine... ;-)

                        CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                        1 Reply Last reply
                        0
                        • K Keith Barrow

                          jamesc69 wrote:

                          If I run in debug mode, on the first pass through ForEach it says that the innertext will be 113 (for the whole occurrence of that node), on the second pass it says that the innertext will be 212 (which is correct).

                          OK, so we've eliminated two possibilities: The xmlDoc.SelectNodes is selecting the node correctly and the loop is iterating over the nodes correctly.

                          jamesc69 wrote:

                          I know this can be a problem with say SelectSingleNode, but I'm not using that. So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document.

                          Correct _xmlnode_["_NodeName_"].InnerText is not iterative, however you are the source _xmlnode_(in your case objRNode) is the result of an interation. So you will get the objRNode["RoomRQId"].InnerText will get the InnerText of the current objRNode's RoomRQId correctly. Both the code I posted earlier shows that to be the case and Luc Pattyn has tested it.

                          jamesc69 wrote:

                          So therefore objRNode["RoomRQId"].InnerText is not iterative, and is always picking the 1st occurence with the document.

                          Not necessarily (see above). It is possible that, as Luc said, SelectHotelAvail() is fouling up somehow. It is possible that the output code is not working as expected, or that somehow the value is being set in another place (e.g. later in the code, concurrency problems). Without full source code this is hard to find. The line intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText); should get the correct Id. I'd use a breakpoint to ensure that correct ID is being set by that line, then step through to see if it changes.

                          CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                          J Offline
                          J Offline
                          jamesc69
                          wrote on last edited by
                          #12

                          Hi, First of all thanks all for your help, but this apparently isn't a coding problem, but actually a problem with my Vis Studio.   My Vis Studio was not building a new dll each time and therefore any change I was making was having no effect.   I created a new solution, added all the existing files into the new solution, and guess what - it now works! Apologies for wasting everyone's time.

                          K 1 Reply Last reply
                          0
                          • J jamesc69

                            Hi, First of all thanks all for your help, but this apparently isn't a coding problem, but actually a problem with my Vis Studio.   My Vis Studio was not building a new dll each time and therefore any change I was making was having no effect.   I created a new solution, added all the existing files into the new solution, and guess what - it now works! Apologies for wasting everyone's time.

                            K Offline
                            K Offline
                            Keith Barrow
                            wrote on last edited by
                            #13

                            Glad you got it sorted!

                            CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                            1 Reply Last reply
                            0
                            • J jamesc69

                              Hi, I have XML in the following structure:    <Rooms>       <Room>          <RoomRQId>1</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>3</NumAdults>       </Room>       <Room>          <RoomRQId>2</RoomRQId>          <Quantity>1</Quantity>          <NumAdults>2</NumAdults>       </Room>    </Rooms> And each time, I get the values assigned as the first occurence of room.   Running in debug mode, I can see the second node called within the For each loop, but the values are still assigned as the second occurence.   Code below: XmlNodeList nodeRooms = xmlDoc.SelectNodes("/test:HotelAvail/test:Rooms/test:Room", ns); foreach (XmlNode objRNode in nodeRooms)                      {                            intRoomRQId = int.Parse(objRNode["RoomRQId"].InnerText);                                 strQuantity = objRNode["Quantity"].InnerText;                            intNumAdults = int.Parse(objRNode["NumAdults"].InnerText); }                So IntRoomRQID is set as 1 twice, when it should create a node of 1, and then a node of value 2.

                              realJSOPR Offline
                              realJSOPR Offline
                              realJSOP
                              wrote on last edited by
                              #14

                              Is there a reason you're not using linq-to-xml? It's much simpler (types from memory, so you may need to tweak it a bit)...

                              XDocument doc = XDocument.Load("myfile.xml");
                              XElement root = doc.Element["Rooms"];
                              foreach (XElement element in root.Elements)
                              {
                              int roomID = Convert.ToInt32(element.Element["RoomRQID"].Value);
                              int quantity = Convert.ToInt32(element.Element["Quantity"].Value);
                              int numAdults = Convert.ToInt32(element.Element["NumAdults"].Value);
                              }

                              No messing around with InnerText and that kind of crap.

                              .45 ACP - because shooting twice is just silly
                              -----
                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                              -----
                              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups