Search code examples
c#xmlxelement

how get the value of second child element in xml using XElement in c#


I want to get the value of the second price element.we cant get last or first directly.but I don't have an idea how to get the second element directly. this is the xml....( I put only the needed part.my xml is valid)

<Departure>
    <Date>2016-09-24T00:00:00.0000000</Date>
    <Pricing>
      <Price>
        <Type>ADT</Type>
        <Value>299.00</Value>
        <Qty>20</Qty>
      </Price>
      <Price>
        <Type>CHD</Type>
        <Value>230.00</Value>
        <Qty>5</Qty>
      </Price>
      <Price>
        <Type>INF</Type>
        <Value>150.00</Value>
        <Qty>5</Qty>
      </Price>
      <Price>
        <Type>FAM</Type>
        <Value>0.00</Value>
        <Qty>0</Qty>
      </Price>
      <Price>
        <Type>SEN</Type>
        <Value>299.00</Value>
        <Qty>20</Qty>
      </Price>
    </Pricing>
  </Departure>
  <Departure>

this is how I took the Last()

 numberofDays = itm.Elements("Itinerary").Elements("ItineraryItem").Elements("EndDay").Last().Value,

like this I want to get the value of the second price element.(which the Type valud is "CHD")


Solution

  • To get an element at position N you could skip N-1 elements, and take FirstOrDefault:

    var secondPrice = item.GetElements("Price").Skip(1).FirstOrDefault();