Search This Blog

Tuesday, July 2, 2013

Read 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)
    {
          readXML_by_linq();
        //readXML_by_linq_Xdoc();
        //get_one_Element();
        //get_many_Element();
        //get_Specific_result();
        //get_other_element_result();
        //get_desending_order();
        //get_price_shorting();
        //create_xml();
        //test();
    }

    private void readXML_by_linq()
    {
        XElement xelement = XElement.Load(Server.MapPath("~/xml/hotel.xml"));

        /*An IEnumerable generic interface is returned from query expressions.
         A query expression that selects ints will be of type IEnumerable<int>.
         On an IEnumerable variable, you can also use the foreach-loop.*/

        IEnumerable<XElement> hotelDetail = xelement.Elements();

        // Read the XML

        foreach (var hotel in hotelDetail)
        {
            Response.Write(hotel);

        }

    }



Input XML file Data:

<?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>
  <Hotel>
    <Code>88932</Code>
    <Name>Orient Prince Hotel</Name>
    <Description />
    <Address>Amine Mneimne St. </Address>
    <MainPic />
    <CountryCode>282</CountryCode>
    <DestCode>7</DestCode>
    <CategoryCode>4</CategoryCode>
    <MultiBookingBoardType>true</MultiBookingBoardType>
    <ReferenceNB>7-88932</ReferenceNB>
    <RoomRates>
      <RoomPurchaseToken>734532202</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Classic Rooms  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>139</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532203</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Deluxe Rooms  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>158</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532204</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Premium Rooms  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>176</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532205</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Junior Suites  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>223</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532207</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Executive Suites Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>295</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>On Request</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532206</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Family Suites  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>316</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>On Request</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
  </Hotel>

Output:
20054 Le Marly
Hamra, Main Street, Beirut, Lebanon.
282 7 3 true 7-20054 720054124 sgl Single Classic Room Bed and Breakfast 1 0 1 101 USD Available 20054 35 Charles
Rustom Bacha Street, Ain El Mraysseh, Beirut
282 7 3 true 7-35 73516 sgl Single Standard Room Bed and Breakfast 1 0 1 127 USD Available 35 73517 sgl Single Deluxe Room Bed and Breakfast 1 0 1 127 USD Available 35 88932 Orient Prince Hotel
Amine Mneimne St. 



No comments :