Search This Blog

Tuesday, July 2, 2013

Get short price list from xml file by linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
//***Add below class for XElement
using System.Xml;
using System.Text;
using System.Xml.Linq;
/*LINQ—language integrated query—introduces many extensions methods to the standard C# environment. 
  These methods work on Lists, arrays and collections that are not yet in memory. 
*/

public partial class Linq : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
        get_price_shorting();
      


    private void get_price_shorting()
    {
        XElement xelement = XElement.Load(Server.MapPath("~/xml/hotel.xml"));
        IEnumerable<string> prices = from price in xelement.Elements("Hotel")
                                     let rate = (string)price.Element("RoomRates").Element("Price")
                                     orderby rate
                                     select rate;
        Response.Write("List and Sort all Hotel Price: ");
        foreach (string p in prices)
        Response.Write(p + "</br>");

    }
}
INPUT XML FILE:

<?xml version="1.0" encoding="utf-8"?>
<HotelRoomAvailability>
  <Hotel>
    <Code>20054</Code>
    <Name>Le Marly</Name>
    <Description />
    <Address>Hamra, Main Street, Beirut, Lebanon.</Address>
    <MainPic />
    <CountryCode>282</CountryCode>
    <DestCode>7</DestCode>
    <CategoryCode>3</CategoryCode>
    <MultiBookingBoardType>true</MultiBookingBoardType>
    <ReferenceNB>7-20054</ReferenceNB>
    <RoomRates>
      <RoomPurchaseToken>720054124</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Classic Room Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>101</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>20054</HotelCode>
    </RoomRates>
  </Hotel>
  <Hotel>
    <Code>35</Code>
    <Name>Charles</Name>
    <Description />
    <Address>Rustom Bacha Street, Ain El Mraysseh, Beirut</Address>
    <MainPic />
    <CountryCode>282</CountryCode>
    <DestCode>7</DestCode>
    <CategoryCode>3</CategoryCode>
    <MultiBookingBoardType>true</MultiBookingBoardType>
    <ReferenceNB>7-35</ReferenceNB>
    <RoomRates>
      <RoomPurchaseToken>73516</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Standard Room Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>127</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>35</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>73517</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Deluxe Room  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>127</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>35</HotelCode>
    </RoomRates>
  </Hotel>


OUTPUT:
List and Sort all Hotel Price: 101
127
139
139
148
153
160
167

No comments :