Search This Blog

Wednesday, July 31, 2013

Merge Two XML Sheet By XSLT

CS page code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Configuration;

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

        var xslt = new XslCompiledTransform();
        xslt.Load(Server.MapPath("~/merge.xslt"), new XsltSettings{EnableDocumentFunction = true}, null );
        var xmlr = XmlReader.Create(Server.MapPath("~/A.xml"));
        var xmlw = XmlWriter.Create(Server.MapPath("~/result.htm"));
        var xslargs = new XsltArgumentList();
        xslargs.AddParam("fileName", "", "B.xml");
        xslt.Transform(xmlr, xslargs , xmlw);
        xmlw.Close();
        xmlr.Close();
        Response.Redirect("result.htm");
    }
        
}

A.XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Result>
  <A>
  <Id>A-001</Id>
  <Title1>A1 file</Title1>
  <Description>This is A1 xml file</Description>
    </A>
  <A>
  <Id>A-002</Id>
  <Title1>A2 file</Title1>
  <Description>This is A2 xml file</Description>
  </A>
  <A>
  <Id>A-003</Id>
  <Title1>A3 file</Title1>
  <Description>This is A3 xml file</Description>
  </A>
  <A>
  <Id>A-004</Id>
  <Title1>A4 file</Title1>
  <Description>This is A4 xml file</Description>
  </A>
</Result> 

B.XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Result>
  <A>
  <Id>B-001</Id>
  <Title1>B1 file</Title1>
  <Description>This is B1 xml file</Description>
  </A>
  <A>
  <Id>B-002</Id>
  <Title1>B2 file</Title1>
  <Description>This is B2 xml file</Description>
  </A>
  <A>
  <Id>B-003</Id>
  <Title1>B3 file</Title1>
  <Description>This is B3 xml file</Description>
  </A>
</Result>

merge.xslt file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:param name="fileName" select="'Result/*'" />
<xsl:param name="updates" select="document($fileName)" />
<xsl:variable name="updateItems" select="$updates/*" />
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="/">

  <html>
    <body>
      <h2>Merge Two XML Sheet By XSLT</h2>
  <merge>
    <xsl:copy>
      <xsl:apply-templates select="Result/A"/>
      <xsl:apply-templates select="$updateItems"/>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>ID</th>
            <th>Title</th>
            <th>Description</th>
          </tr>
         
     <xsl:for-each select="Result/A">
      <tr>
        <td>
          <xsl:value-of select="Id"/>
        </td>
        <td>
          <xsl:value-of select="Title1"/>
        </td>
        <td>
          <xsl:value-of select="Description"/>
        </td>
      </tr>
      </xsl:for-each>
   
    </table>
    </xsl:copy>
  </merge>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

OUTPUT:



How can convert XML file in to HTML format using by XSLT file.

What is XSLT ?

XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.
XSLT stands for XSL Transformations. In this tutorial you will learn how to use XSLT to transform XML documents into other formats, like XHTML.


xsl_convert_html.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xsl_convert_html.aspx.cs" Inherits="xsl_convert_html" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ask-dotnet.blogspot.in/">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

xsl_convert_html.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Configuration;

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

    public void xsl()
    {

        //XmlReader loads XML file
        XmlReader reader = XmlReader.Create(Server.MapPath("~/xmlfile.xml"));

        //XmlTextWriter creats output file
        XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/xmlfile.htm"), null);

        //XslCompiledTransform loads XSLT file
        XslCompiledTransform xsl = new XslCompiledTransform();
        xsl.Load(Server.MapPath("~/test.xsl"));
        xsl.Transform(reader, writer);
        writer.Close();
        Response.Redirect("xmlfile.htm");

    }
}

XML File:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<category>
  <list>
    <sno>1</sno>
    <hotel>Uday Palace Hotel</hotel>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </list>
  <list>
    <sno>2</sno>
    <hotel>Hotel Sunstar Residency</hotel>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </list>
  <list>
    <sno>3</sno>
    <hotel>Aura De Asia Hotel</hotel>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </list>
</category>

test.xsl file:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ask-dotnet.blogspot.in/">

  <xsl:template match="/">
    <html>
      <body>
        <h2>Hotel  List</h2>
        <table border="1">
          <tr bgcolor="#FFCCFF">
            <th>Sno</th>
            <th>Hotel</th>
            <th>Country</th>
          </tr>
          <xsl:for-each select="category/list">
            <tr>
              <td>
                <xsl:value-of select="sno"/>
              </td>
              <td>
                <xsl:value-of select="hotel"/>
              </td>
              <td>
                <xsl:value-of select="country"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

OUTPUT:



Template text file with a .tt file extension in c#

The basic idea of the template toolkit is to parse an input file and transform it into an output file. The input file is a template—a text file with a .tt file extension. The output file will also contain text, and the text can be C# code, Visual Basic code, Web Forms code.

How can create .tt file in asp.net ???


Add text.tt file:





Input of text.tt file:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
    <#int top = 10;
       for(int i = 0; i<=top; i++) { #>
    The square of <#= i #> is <#= i*i #>
    <# } #>

Output of text.txt file:

     The square of 0 is 0
        The square of 1 is 1
        The square of 2 is 4
        The square of 3 is 9
        The square of 4 is 16
        The square of 5 is 25
        The square of 6 is 36
        The square of 7 is 49
        The square of 8 is 64
        The square of 9 is 81
        The square of 10 is 100

Friday, July 19, 2013

Jave script code for div show or hide depend on drop-down value

Jave script code for div show or hide depend on drop-down value: 

OUT PUT: 





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body onload="clear()";>
<script type="text/jscript">
    function checktable() {
     
        if (document.form1.ddlist.value == 0) {
            document.all.div1.style.display = "none";
            document.all.div2.style.display = "none";
            document.all.div3.style.display = "none";
            document.all.div4.style.display = "none";
         
        }
        if (document.form1.ddlist.value == 1)
         {
             document.all.div1.style.display = "inline";
             document.all.div2.style.display = "none";
             document.all.div3.style.display = "none";
             document.all.div4.style.display = "none";
         
        }
        if (document.form1.ddlist.value == 2)
        {
            document.all.div1.style.display = "inline";
            document.all.div2.style.display = "inline";
            document.all.div3.style.display = "none";
            document.all.div4.style.display = "none";
       
        }
        if (document.form1.ddlist.value == 3) {
            document.all.div1.style.display = "inline";
            document.all.div2.style.display = "inline";
            document.all.div3.style.display = "inline";
            document.all.div4.style.display = "none";
         
        }
        if (document.form1.ddlist.value == 4) {
            document.all.div1.style.display = "inline";
            document.all.div2.style.display = "inline";
            document.all.div3.style.display = "inline";
            document.all.div4.style.display = "inline";
       
        }
    }

    function showChild1() {
   
        if (document.form1.child1.value == 0) {
            document.all.child1_1.style.display = "none";
            document.all.child1_2.style.display = "none";

        }
        if (document.form1.child1.value == 1)
        {
            document.all.child1_1.style.display = "inline";
            document.all.child1_2.style.display = "none";
         
        }
        if (document.form1.child1.value == 2)
        {
            document.all.child1_1.style.display = "inline";
            document.all.child1_2.style.display = "inline";
         
        }
    }

    function showChild2() {
   
        if (document.form1.child2.value == 0) {
            document.all.child2_1.style.display = "none";
            document.all.child2_2.style.display = "none";

        }
        if (document.form1.child2.value == 1) {
            document.all.child2_1.style.display = "inline";
            document.all.child2_2.style.display = "none";

        }
        if (document.form1.child2.value == 2) {
            document.all.child2_1.style.display = "inline";
            document.all.child2_2.style.display = "inline";

        }
    }
    function showChild3() {
   
        if (document.form1.child3.value == 0) {
            document.all.child3_1.style.display = "none";
            document.all.child3_2.style.display = "none";

        }
        if (document.form1.child3.value == 1) {
            document.all.child3_1.style.display = "inline";
            document.all.child3_2.style.display = "none";

        }
        if (document.form1.child3.value == 2) {
            document.all.child3_1.style.display = "inline";
            document.all.child3_2.style.display = "inline";

        }
    }
    function showChild4() {
   
        if (document.form1.child4.value == 0) {
            document.all.child4_1.style.display = "none";
            document.all.child4_2.style.display = "none";

        }
        if (document.form1.child4.value == 1) {
            document.all.child4_1.style.display = "inline";
            document.all.child4_2.style.display = "none";

        }
        if (document.form1.child4.value == 2) {
            document.all.child4_1.style.display = "inline";
            document.all.child4_2.style.display = "inline";

        }
    }

</script>
<form id="form1" name="form1" action="">
    <table class="style1">
     

        <tr>
            <td>
                <select id="ddlist" name="ddlist" onchange="checktable();">
                 <option value=0 selected="selected">Select Room</option>
                    <option value=1>1</option>
                     <option value=2>2</option>
                     <option value=3>3</option>
                     <option value=4>4</option>
                </select></td>
            <td>
                &nbsp;</td>
        </tr>
     

        <tr >
            <td>
                <div id="div1" style="display:none">
                <table class="style1" id="tb1">
                   <tr>
<td>
                                                <div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Room</a></label>
<label>
Room:1</label>
</div>
<div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Adults</a></label>
<select id="field_form1_room_adult" name="field_form1_room_adult" class="small field_form1_room_adult#index#">
<option value="1" selected="selected">1</option>
<option value="2" >2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div style="float:left;padding-right:1px; display:block;" id="divc1">
<label for="sheepItForm_#index#_child"><a href="#" onclick="javascript:return false;" title="Number of Children(0-2)">Child</a></label>
<select id="child1" name="child1" class="small field_form1_room_#index#_child" onchange="showChild1();">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div style="float:left;padding-right:1px;display:none;" id="child1_1">
<label for="sheepItForm_#index#_fage"><a href="#" onclick="javascript:return false;" title="Age of child 1 (0-18)"> Ch1 age</a><span id="Span1"></span></label>
<select id="field_form1_fage" name="field_form1_fage"  class="small field_form1_fage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</select>
</div>
<div style="float:left; padding-right:1px;display:none;" id="child1_2">
<label for="sheepItForm_#index#_sage"><a href="#" onclick="javascript:return false;" title="Age of child 2 (0-18)"> Ch2 age</a> <span id="Span2"></span></label>
<select id="field_form1_sage" name="field_form1_sage" class="small field_form1_sage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</select>
</div>
<div style="float:left;padding-top:20px;">
<a id="sheepItForm_remove_current" href="#">
<img class="delete" src="/images/cross.png" width="16" height="16" border="0" />
</a>
</div>
</td>
</tr>
                </table>
                </div>
            </td>
            <td>
                &nbsp;</td>
        </tr>
     

        <tr>
            <td><div id="div2" style="display:none">
                <table class="style1"  id="tb2">
                   <tr>
<td>
                                                <div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Room</a></label>
<label>
Room:2</label>
</div>
<div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Adults</a></label>
<select id="Select1" name="field_form1_room_adult" class="small field_form1_room_adult#index#">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div style="float:left;padding-right:1px;" >
<label for="sheepItForm_#index#_child"><a href="#" onclick="javascript:return false;" title="Number of Children(0-2)">Child</a></label>
<select id="child2" name="child2" class="small field_form1_room_#index#_child" onchange="showChild2();">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div style="float:left;padding-right:1px;display:none;" id="child2_1">
<label for="sheepItForm_#index#_fage"><a href="#" onclick="javascript:return false;" title="Age of child 1 (0-18)"> Ch1 age</a><span id="Span3"></span></label>
<select id="Select3" name="field_form1_fage"  class="small field_form1_fage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>

</select>
</div>
<div style="float:left; padding-right:1px;display:none;" id="child2_2">
<label for="sheepItForm_#index#_sage"><a href="#" onclick="javascript:return false;" title="Age of child 2 (0-18)"> Ch2 age</a> <span id="Span4"></span></label>
<select id="Select4" name="field_form1_sage" class="small field_form1_sage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>

</select>
</div>
<div style="float:left;padding-top:20px;">
<a id="A1" href="#">
<img class="delete" src="/images/cross.png" width="16" height="16" border="0" />
</a>
</div>
</td>
</tr>
                </table></div>
            </td>
            <td>
                &nbsp;</td>
        </tr>
          <tr>
            <td><div id="div3" style="display:none">
                <table class="style1"  id="tb3">
                   <tr>
<td>
                                                <div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Room</a></label>
<label>
Room:3</label>
</div>
<div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Adults</a></label>
<select id="Select5" name="field_form1_room_adult" class="small field_form1_room_adult#index#">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_child"><a href="#" onclick="javascript:return false;" title="Number of Children(0-2)">Child</a></label>
<select id="child3" name="child3" class="small field_form1_room_#index#_child" onchange="showChild3();">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div style="float:left;padding-right:1px;display:none;" id="child3_1">
<label for="sheepItForm_#index#_fage"><a href="#" onclick="javascript:return false;" title="Age of child 1 (0-18)"> Ch1 age</a><span id="Span5"></span></label>
<select id="Select7" name="field_form1_fage"  class="small field_form1_fage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</select>
</div>
<div style="float:left; padding-right:1px;display:none;" id="child3_2">
<label for="sheepItForm_#index#_sage"><a href="#" onclick="javascript:return false;" title="Age of child 2 (0-18)"> Ch2 age</a> <span id="Span6"></span></label>
<select id="Select8" name="field_form1_sage" class="small field_form1_sage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</select>
</div>
<div style="float:left;padding-top:20px;">
<a id="A2" href="#">
<img class="delete" src="/images/cross.png" width="16" height="16" border="0" />
</a>
</div>
</td>
</tr>
                </table></div>
            </td>
            <td>
                &nbsp;</td>
        </tr>
          <tr>
            <td><div id="div4" style="display:none">
                <table class="style1"  id="tb4">
                   <tr>
<td>
                                                <div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Room</a></label>
<label>
Room:4</label>
</div>
<div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_adult"><a href="#" onclick="javascript:return false;" title="Number of Adults(1-4)">Adults</a></label>
<select id="Select9" name="field_form1_room_adult" class="small field_form1_room_adult#index#">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div style="float:left;padding-right:1px;">
<label for="sheepItForm_#index#_child"><a href="#" onclick="javascript:return false;" title="Number of Children(0-2)">Child</a></label>
<select id="child4" name="child4" class="small field_form1_room_#index#_child" onchange="showChild4();">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div style="float:left;padding-right:1px;display:none;" id="child4_1">
<label for="sheepItForm_#index#_fage"><a href="#" onclick="javascript:return false;" title="Age of child 1 (0-18)"> Ch1 age</a><span id="Span7"></span></label>
<select id="Select11" name="field_form1_fage"  class="small field_form1_fage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</select>
</div>
<div style="float:left; padding-right:1px;display:none;" id="child4_2">
<label for="sheepItForm_#index#_sage"><a href="#" onclick="javascript:return false;" title="Age of child 2 (0-18)"> Ch2 age</a> <span id="Span8"></span></label>
<select id="Select12" name="field_form1_sage" class="small field_form1_sage#index#">
<option value="" selected="selected"></option>
<option value="0" >0</option>
<option value="1">1</option>
<option value="2">2</option>

</select>
</div>
<div style="float:left;padding-top:20px;">
<a id="A3" href="#">
<img class="delete" src="/images/cross.png" width="16" height="16" border="0" />
</a>
</div>
</td>
</tr>
                </table></div>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </form>
</body>
</html>

Saturday, July 13, 2013

Non-invocable member 'System.Configuration.ConfigurationManager.AppSettings' cannot be used like a method.

Non-invocable member 'System.Configuration.ConfigurationManager.AppSettings' cannot be used like a method.

Web.config file code:
<appSettings>
    <!-- Custom settings for application -->
    <add key="AppName" value="askdotnet.com" />
    <add key="AppTitle" value="Welcome to askdotnet.com" />
    <add key="AUTHORIZE_NET_API_LOGIN" value="http://askdotnet.com" />
    <add key="ServicesServer" value="http://askdotnet.com" />
    <add key="XBEServer" value="http://askdotnet.com" />
  </appSettings>
cs page code:

Error no 1.
Non-invocable member 'System.Configuration.ConfigurationManager.AppSettings' cannot be used like a method.
Issue no 1.
strAuthorizeNet_x_login = System.Configuration.ConfigurationManager.AppSettings("AUTHORIZE_NET_API_LOGIN");
Solutions no 1:
strAuthorizeNet_x_login = System.Configuration.ConfigurationManager.AppSettings["AUTHORIZE_NET_API_LOGIN"];

Error no 2.
This method is absolete....
Issue no 2.
strAuthorizeNet_x_login = System.Configuration.ConfigurationSettings.AppSettings("AUTHORIZE_NET_API_LOGIN");
Solutions no 2:
strAuthorizeNet_x_login = System.Configuration.ConfigurationManager.AppSettings["AUTHORIZE_NET_API_LOGIN"];


Tuesday, July 2, 2013

How can use Group by in linq with XML file

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)
    {
      
        test();
    }

   private void test()
    {
        XDocument doc = XDocument.Parse(@"<root>
  <RoomRates>
      <RoomPurchaseToken>73516</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Standard Room Bed and Breakfast</RoomType>
      <Adults>45</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>1255</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>
 <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>
</root>");

        var query =
                from RoomRates in doc.Root.Elements("RoomRates")
                group RoomRates by (int)RoomRates.Element("HotelCode") into g1
                select new
                {
                    HotelCode = g1.Key,
                    AllValues = from alldata in g1
                                //from alldata in g1.Elements("Price")
                                //select (string)alldata
                                select new 
                                {
                                    price = (string)alldata.Element("Price"),
                                    Children = (string)alldata.Element("Children"),
                                    CurrencyCode = (string)alldata.Element("CurrencyCode"),
                                }
                };
        

            foreach (var item in query)
            {
                Response.Write("Key:" + item.HotelCode+ "</br>");
                foreach (var alldata in item.AllValues)
                {
                    Response.Write(alldata + "</br>");
                }
                Console.WriteLine();
            }
        }
    }
OUTPUT:

Key:35
{ price = 1255, Children = 0, CurrencyCode = USD }
{ price = 127, Children = 0, CurrencyCode = USD }
Key:88932
{ price = 158, Children = 0, CurrencyCode = USD }
{ price = 176, Children = 0, CurrencyCode = USD }
{ price = 223, Children = 0, CurrencyCode = USD }
{ price = 295, Children = 0, CurrencyCode = USD }

Create XML file on local system

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)
    {

        create_xml();
   
    }

   private void create_xml()
    {
        XNamespace empNM = "urn:lst-hotel:hotel";

        XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-16", null),
                    new XElement(empNM + "HotelRoomAvailability",
                        new XElement("Hotel",
                            new XComment("Hotel element in below:"),
                            new XElement("Code", "5"),
                            new XElement("Name", "Le Marly"),
                            new XElement("Address", "Hamra, Main Street, Beirut, Lebanon.")
                            )));

        StringWriter sw = new StringWriter();
        XmlWriter xWrite = XmlWriter.Create(sw);
        xDoc.Save(xWrite);
        xWrite.Close();

        // Save to Disk
        xDoc.Save(Server.MapPath("~/xml/hotel1.xml"));
        Response.Write("XML save in disk");
    }
}
OUTPUT:
XML save in disk 

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

Get Descending order all records 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_desending_order();
     
    }

    private void get_desending_order()
    {
        XElement xelement = XElement.Load(Server.MapPath("~/xml/hotel.xml"));
        foreach (XElement xele in xelement.Descendants("Price"))
        {
            Response.Write("</br>");
            Response.Write((string)xele);
        }

    }
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:
101
127
127
139
158
176
223
295
316
139
148
153
278
399
160
186
207

Get element on the bases of condition 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_other_element_result();
      
    }

  
    private void get_other_element_result()
    {
        XElement xelement = XElement.Load(Server.MapPath("~/xml/hotel.xml"));
        var Hotel = from hname in xelement.Elements("Hotel")
                    where (int)xelement.Element("Hotel").Element("Code") == (int)hname.Element("RoomRates").Element("HotelCode")
                    select hname;
        Console.WriteLine("Details of Hotel Name:");
        foreach (XElement xele in Hotel)
            Response.Write(xele.Element("Name") + "</br>" + xele.Element("Code") + "</br>" + xele.Element("RoomRates").Element("Price") + "</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:
Le Marly
20054
101