tags:

views:

32

answers:

2

I have a node and this node contains 5 childnodes. three of them is RatePlan. How can i select those RatePlan childnodes with LINQ?

Lets clarify something :

my xml is like this :

<hotels>
<hotel id="1" name="hotel 1">
    <telephone>123456789</telephone>
    <fax>123</fax>
    <address>hotels address</address>
    <hotelRatePlan>10</hotelRatePlan>
    <hotelRatePlan>11</hotelRatePlan>
    <hotelRatePlan>12</hotelRatePlan>
  </hotel>
  <hotel id="2" name="hotel 2">
    <telephone>123456789</telephone>
    <fax>123</fax>
    <address>hotels address</address>
    <hotelRatePlan>100</hotelRatePlan>
    <hotelRatePlan>110</hotelRatePlan>
    <hotelRatePlan>120</hotelRatePlan>
  </hotel>
  <hotel id="3" name="hotel 3">
    <telephone>123456789</telephone>
    <fax>123</fax>
    <address>hotels address</address>
    <hotelRatePlan>10</hotelRatePlan>
    <hotelRatePlan>11</hotelRatePlan>
    <hotelRatePlan>12</hotelRatePlan>
  </hotel>
</hotels>

I am using XMLDocument to read XML file. After i read it i make a selection with SelectNodes. When i get first hotel information i want to select specific childnodes (hotelRatePlan). How can i do that?

A: 

Your question isn't particularly clear, but you might just want:

var ratePlans = node.Elements("RatePlan");

That's assuming you're actually using LINQ to XML rather than XmlNode, XmlDocument etc. If you are using the "old" DOM API, you could use:

var ratePlans = node.ChildNodes
                    .OfType<XmlElement>()
                    .Where(e => e.LocalName == "RatePlan");

... but I'd moving to LINQ to XML if you can. It's simply a much nicer XML API.

Jon Skeet
A: 

I think you mean:

var ratePlans = node.ChildNodes.OfType<RatePlan>();

danijels