Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following XML,

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,


C#
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
Posted
Updated 19-Nov-12 2:58am
v2
Comments
n.podbielski 19-Nov-12 9:39am    
And what do you getting?
BeamingJo 20-Nov-12 7:56am    
I've tried your code and I become this:

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.

Is it not what you are expecting to have?

Maybe you're expecting this:

Check In: 3:00 pm

Hotel is situated in the heart of the capital city the hotel is
close to all of the city's major attractions. Centrally located
on New Jersey Avenue close to nearby dining and shopping.

If it is the case, try this:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900