3 Replies - 1428 Views - Last Post: 19 November 2012 - 11:36 PM Rate Topic: -----

#1 vikramsinh_shinde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-February 12

Get Element query in LINQ

Posted 19 November 2012 - 06:13 AM

I have following XML,

<HotelProduct PricingSource="System" InventorySource="System" ExternalDetailsObtained="false" Code="IAI06" Category="4R" InstantPurchase="false">
 <Description>Deluxe Guest Room</Description>
 <Supplier Type="Hotel" Code="LIAIS" City="WAS"/>
<InventoryDetails Status="Available">
<ExtendedInventoryDetails MaximumAvailable="3">
 <InventoryProduct Code="IAI06" City="WAS"/>
 </ExtendedInventoryDetails>
 </InventoryDetails>
 <PriceDetails PricingDuration="5" Price="765.00" IncludesBonusNights="false"/>
<DescriptiveItineraryText>
 <Text Heading="true">Check In: 3:00 pm</Text>
 <Text/>
 <Text>Hotel is situated in the heart of the capital city the hotel is</Text>
 <Text>close to all of the city's major attractions. Centrally located</Text>
 <Text>on New Jersey Avenue close to nearby dining and shopping.</Text>
 </DescriptiveItineraryText>
 <DurationDetails MinimumDuration="1" MaximumDuration="99"/>
<DateBands>
<DateBand EffectiveToDate="2012-11-26" EffectiveFromDate="2012-11-16">
 <Applicability SplitRates="true" BaseRates="false"/>
 <CheckOutDetails StandardCheckOutTime="12:00:00"/>
<MealOptions>
<MealOption Code="0">
 <Description>No Meals Included</Description>
 </MealOption>
 </MealOptions>
 </DateBand>
<DateBand EffectiveToDate="2013-02-10" EffectiveFromDate="2012-11-27">
 <Applicability SplitRates="true" BaseRates="false"/>
 <CheckOutDetails StandardCheckOutTime="12:00:00"/>
 </DateBand>
<DateBand EffectiveToDate="2013-02-25" EffectiveFromDate="2013-02-11">
 <Applicability SplitRates="true" BaseRates="false"/>
 <CheckOutDetails StandardCheckOutTime="12:00:00"/>
 </DateBand>
<DateBand EffectiveToDate="2013-02-27" EffectiveFromDate="2013-02-26">
 <Applicability SplitRates="true" BaseRates="false"/>
 <CheckOutDetails StandardCheckOutTime="12:00:00"/>
 </DateBand>
<DateBand EffectiveToDate="2013-03-17" EffectiveFromDate="2013-02-28">
 <Applicability SplitRates="true" BaseRates="false"/>
 <CheckOutDetails StandardCheckOutTime="12:00:00"/>
 </DateBand>
 </DateBands>
 </HotelProduct>



I wanted to get all values of <Text> in <DescriptiveItineraryText> element and create a string builder.

I have tried following query in LINQ,

IEnumerable<string> textSegs = from seg in _hotel.Descendants("DescriptiveItineraryText")
from text in seg.Descendants("Text")
select (string)text;

string str = textSegs.Aggregate(new StringBuilder(), (sb, i) => sb.Append(i), sp => sp.ToString());



I have declared _hotel as XElement.

I am not getting expected result.

Any suggestion would be helpful.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Get Element query in LINQ

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Get Element query in LINQ

Posted 19 November 2012 - 06:45 AM

If you want to just create a string builder which contains the data you need, instead of creating a new instance of a string builder in Aggregate() function, create it outside a function and pass it as a parameter:

StringBuilder sbx = new StringBuilder();
string str = textSegs.Aggregate(sbx, (sb, i) => sb.Append(i), sp => sp.ToString());



But other than that the code seems to be working just fine. Aggregate function returns a string "Check In: 3:00 pmHotel is situated in the heart of the capital city the hotel isclose to all of the city's major attractions. Centrally locatedon New Jersey Avenue close to nearby dining and shopping."
Was This Post Helpful? 0
  • +
  • -

#3 vikramsinh_shinde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-February 12

Re: Get Element query in LINQ

Posted 19 November 2012 - 07:01 AM

View Postjanne_panne, on 19 November 2012 - 06:45 AM, said:

If you want to just create a string builder which contains the data you need, instead of creating a new instance of a string builder in Aggregate() function, create it outside a function and pass it as a parameter:

StringBuilder sbx = new StringBuilder();
string str = textSegs.Aggregate(sbx, (sb, i) => sb.Append(i), sp => sp.ToString());



But other than that the code seems to be working just fine. Aggregate function returns a string "Check In: 3:00 pmHotel is situated in the heart of the capital city the hotel isclose to all of the city's major attractions. Centrally locatedon New Jersey Avenue close to nearby dining and shopping."


But my LINQ query is returning null, that part of code is not working.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Get Element query in LINQ

Posted 19 November 2012 - 11:36 PM

You are probably get null, because the outer node HotelProduct is not be treated as the container.

Warning VB.net code ahead.
Spoiler

This post has been edited by AdamSpeight2008: 19 November 2012 - 11:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1