Search This Blog

Tuesday, July 2, 2013

Get many element from XML input 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_many_Element();
    
    }

     private void get_many_Element()
    {
        XElement xelement = XElement.Load(Server.MapPath("~/xml/hotel.xml"));
        IEnumerable<XElement> HotelRoomAvailability = xelement.Elements();
        Response.Write("</br>");
        Response.Write("Hotel Name with Hotel Code :");
        Response.Write("</br>");
        foreach (var Hotel in HotelRoomAvailability)
        {
            Response.Write(Hotel.Element("Name").Value);
            Response.Write("</br>");
            Response.Write(Hotel.Element("Code").Value);
            Response.Write("</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:
Hotel Name with Hotel Code :
Le Marly
20054
Charles
35
Orient Prince Hotel
88932
Napoleon
22909
Royal Garden
57909
The Mayflower
22870
Orient Queen Homes
44170
Cosmopolitan Hotel
67022
Bella Riva Suite
26


No comments :