Gerald Oakham
Gerald Oakham

Reputation: 173

c# foreach loop though a XmlNodeList sub nodes

I have this XML file

<?xml version="1.0" encoding="utf-8"?>
<message>
  <success/>
  <bookings>
    <booking>
      <rooms>
        <room roomCode ="101" uniqueId="abc">
          <stays>
            <stay usedfrom="9:30" usedto="10:30" quantity="1" Price="62.5" rateCode="1"/>
          </stays>
          <extras>
            <extra from="9:30" to="10:30" unitPrice="5.5" extraCode="coffee" quantity="1" inclusive="0"/>
          </extras>
          <guests>
            <guest firstName="John" lastName="Doe" title="MR" ageRange="0"/>
          </guests>
        </room>
        <room roomCode ="Brd" uniqueId="xyz">
          <stays>
            <stay usedfrom="13:30" usedto="15:30" quantity="1" unitPrice="60.0000" rateCode="RACK"/>
          </stays>
          <guests>
            <guest firstName="Jean" lastName="Doe" title="MRS" ageRange="0"/>
          </guests>
        </room>
      </rooms>
    </booking>
  </bookings>
</message>

and I'm trying to get the node, and the loop though all the (NO s) nodes underneath (and there sub nodes). I've kinda done this, but it list all the attributes each loop.

foreach (XmlNode Roomnode in RoomS)
{
    XmlNodeList Room = doc.GetElementsByTagName("room");
    var RoomCount = Room.Count;
    Console.WriteLine("This Booking contains " + RoomCount + " room(s)");

    foreach (XmlNode rmxn in Room)
    {
        RoomCode = rmxn.Attributes["roomCode"].Value;
        UniqueId = rmxn.Attributes["uniqueId"].Value;
        Console.WriteLine("         Room Type: " + RoomCode);
        Console.WriteLine("         Room id: " + UniqueId);

        XmlNodeList Stay = doc.GetElementsByTagName("stay");

        foreach (XmlNode syxn in Stay)
        {
            RateCode = syxn.Attributes["rateCode"].Value;
            Quantity = syxn.Attributes["quantity"].Value;
            UnitPrice = Math.Round(Convert.ToDecimal(syxn.Attributes["unitPrice"].Value), 2);
            FromTime = syxn.Attributes["usedfrom"].Value;
            ToTime = syxn.Attributes["usedto"].Value;
            Console.WriteLine("      Staying from " + FromTime + " to " + ToTime);
            Console.WriteLine("      Price Per Day : " + Currency + UnitPrice);
            Console.WriteLine("      Using Rate Code: " + RateCode);
        }
    }
}

I'm (99.99%) sure that its because of the line

XmlNodeList Stay = doc.GetElementsByTagName("stay");

Now, in JQUERY, I would have replaced "doc" with "this"(instead of getting the whole list again, but "this" doesn't work ("static property" message from VS). Can someone please tell  me what I need to do to change it from this:

     Room Type: 101
     Room id: abc
  Staying from 9:30 to 10:30
  Price Per Day : 62.5
  Using Rate Code: 1
  Staying from 13:20 to 15:30
  Price Per Day : 60.00
  Using Rate Code: RACK
     Room Type: Brd
     Room id: xyz
   Staying from 9:30 to 10:30
  Price Per Day : 62.5
  Using Rate Code: 1
  Staying from 13:20 to 15:30
  Price Per Day : 60.00
  Using Rate Code: RACK

to this

     Room Type: 101
     Room id: abc
  Staying from 9:30 to 10:30
  Price Per Day : 62.5
  Using Rate Code: 1
     Room Type: Brd
     Room id: xyz
  Staying from 13:20 to 15:30
  Price Per Day : 60.00
  Using Rate Code: RACK

Thanks

Upvotes: 1

Views: 2376

Answers (2)

Gerald Oakham
Gerald Oakham

Reputation: 173

I think I have fixed it my replacing

XmlNodeList Stay = doc.GetElementByTagName("stay") ;

with the follow two lines

XmlNode oNode = rmxn.SelectSingleNode(".//stays");
XmlNodeList Stay = oNode.SelectNodes("stay"); 

Thanks for everyone that helped, and I hope this helps anyone who is having the same problem in the future.

Upvotes: 0

Jay Buckman
Jay Buckman

Reputation: 578

You are asking for all stay elements, not the ones in the current room:

XmlNodeList Stay = doc.GetElementsByTagName("stay");

should be

XmlNodeList Stay = room.GetElementsByTagName("stay");

Upvotes: 1

Related Questions