KEMBAR78
Day 68 LINQ To XML (XLINQ) | PDF | Language Integrated Query | Information Technology
0% found this document useful (0 votes)
12 views6 pages

Day 68 LINQ To XML (XLINQ)

This document is a tutorial for beginners on ASP.NET, specifically focusing on LINQ to XML. It provides a step-by-step guide on how to create a web application that displays travel locations using XML data, including code snippets for setting up the project and handling user interactions. The tutorial covers creating a dropdown for continents and displaying corresponding locations with videos and images in a repeater control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Day 68 LINQ To XML (XLINQ)

This document is a tutorial for beginners on ASP.NET, specifically focusing on LINQ to XML. It provides a step-by-step guide on how to create a web application that displays travel locations using XML data, including code snippets for setting up the project and handling user interactions. The tutorial covers creating a dropdown for continents and displaying corresponding locations with videos and images in a repeater control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASP.NET Tutorial By Coder Baba www.youtube.

com/coderbaba

ASP.NET Tutorial-2022

Crash Course for Beginners (Complete Course)


Become ZERO to HERO in ASP.NET
Day 68 Agenda: Class #068

Topics (What’s in it for you)

Introduction to Dot Net Framework

✓ What is LINQ to XML

1
ASP.NET Tutorial By Coder Baba www.youtube.com/coderbaba

LINQ to XML (XLINQ)


Using this we can write queries on XML Source or XML Data just like we have written queries on Arrays
and Collections. To work with LINQ to XML first we need to import the namespace “System.Xml.Linq” which
provides all the classes for writing queries on XML Source. In this namespace we are provided with a class known
as “XElement” which is actually responsible for loading an XML Document into our application and then we need
to write the query on loaded document. To test this first add a XML File under the project naming it as “Travel.xml”
and write the following code in it:

<Locations>
<Location>
<Name>Bangkok</Name>
<Continent>Asia</Continent>
<Video>Bangkok.mp4</Video>
<Image>Bangkok.jpg</Image>
<Price>25000</Price>
</Location>
<Location>
<Name>Dubai</Name>
<Continent>Asia</Continent>
<Video>Dubai.mp4</Video>
<Image>Dubai.jpg</Image>
<Price>22000</Price>
</Location>
<Location>
<Name>Hong Kong</Name>
<Continent>Asia</Continent>
<Video>HongKong.mp4</Video>
<Image>HongKong.jpg</Image>
<Price>35000</Price>
</Location>
<Location>
<Name>London</Name>
<Continent>Europe</Continent>
<Video>London.mp4</Video>
<Image>London.jpg</Image>
<Price>50000</Price>
</Location>
<Location>
<Name>Mumbai</Name>
<Continent>Asia</Continent>
<Video>Mumbai.mp4</Video>
<Image>Mumbai.jpg</Image>

2
ASP.NET Tutorial By Coder Baba www.youtube.com/coderbaba

<Price>15000</Price>
</Location>
<Location>
<Name>New York</Name>
<Continent>North America</Continent>
<Video>NewYork.mp4</Video>
<Image>NewYork.jpg</Image>
<Price>60000</Price>
</Location>
<Location>
<Name>Paris</Name>
<Continent>Europe</Continent>
<Video>Paris.mp4</Video>
<Image>Paris.jpg</Image>
<Price>45000</Price>
</Location>
<Location>
<Name>Rio de Janeiro</Name>
<Continent>South America</Continent>
<Video>RiodeJaneiro.mp4</Video>
<Image>RiodeJaneiro.jpg</Image>
<Price>55000</Price>
</Location>
<Location>
<Name>Singapore</Name>
<Continent>Asia</Continent>
<Video>Singapore.mp4</Video>
<Image>Singapore.jpg</Image>
<Price>30000</Price>
</Location>
<Location>
<Name>Sydney</Name>
<Continent>Australia</Continent>
<Video>Sydney.mp4</Video>
<Image>Sydney.jpg</Image>
<Price>75000</Price>
</Location>
</Locations>
Now create 2 folders under the project naming them as “Images” and “Videos”. Copy an image
corresponding to each city into Images folder and a video corresponding of each city into Videos folder. Add a new
WebForm under the project naming it as “Travel.aspx”. Drag and Drop the “VideoPlayer” (we created this earlier in
Controls Development Classes) control on to the Web Form and delete it and when we do this it will write the
below statement on top of the page:
<%@ Register Assembly="AspControls" Namespace="AspControls" TagPrefix="cc1" %>

Now write the following code under the <div> tag of the Web Form:

3
ASP.NET Tutorial By Coder Baba www.youtube.com/coderbaba

<h1 style="background-color: yellow; color: red; text-align: center">Travel Analog</h1>


<table align="center">
<tr>
<td align="center">
<asp:DropDownList ID="ddlContinents" runat="server" AutoPostBack="true" />
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptLocations" runat="server">
<HeaderTemplate>
<table border="1" align="center">
<tr>
<th>Location</th> <th>Continent</th> <th>Video</th> <th>Image</th> <th>Price</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Name") %></td>
<td><%# Eval("Continent") %></td>
<td>
<cc1:VideoPlayer ID="VideoPlayer1" runat="server" Width="300" Height="200" Controls="true"
Mp4Url='<%# "/Videos/" + Eval("Video") %>' />
</td>
<td>
<asp:Image ID="Image1" runat="server" Width="200" Height="200"
ImageUrl='<%# "/Images/" + Eval("Photo") %>' />
</td>
<td align="right"><%# Eval("Price") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</table>
Now write the below code under “Travel.aspx.cs” file:
using System.Xml.Linq;
Code under Page_Load Event:
if (!IsPostBack) {
var data = XElement.Load(Server.MapPath("Travel.xml"));
var table = data.Elements("Location");
var Continents = from t in table select new { Continent = t.Element("Continent").Value };
ddlContinents.DataSource = Continents.Distinct();

4
ASP.NET Tutorial By Coder Baba www.youtube.com/coderbaba

ddlContinents.DataTextField = "Continent";
ddlContinents.DataValueField = "Continent";
ddlContinents.DataBind();
ddlContinents.Items.Insert(0, "-Select Continent-");
rptLocations.DataSource = from t in table select new { Name = t.Element("Name").Value,
Continent = t.Element("Continent").Value, Video = t.Element("Video").Value,
Photo = t.Element("Image").Value, Price = t.Element("Price").Value };
rptLocations.DataBind();
}
Code under DropDownList SelectedIndexChanged Event:
var data = XElement.Load(Server.MapPath("Travel.xml"));
var table = data.Elements("Location");
if(ddlContinents.SelectedIndex == 0) {
rptLocations.DataSource = from t in table select new { Name = t.Element("Name").Value,
Continent = t.Element("Continent").Value, Video = t.Element("Video").Value,
Photo = t.Element("Image").Value, Price = t.Element("Price").Value };
}
else {
rptLocations.DataSource = from t in table where t.Element("Continent").Value ==
ddlContinents.SelectedValue select new { Name = t.Element("Name").Value,
Continent = t.Element("Continent").Value, Video = t.Element("Video").Value,
Photo = t.Element("Image").Value, Price = t.Element("Price").Value };
}
rptLocations.DataBind();

5
ASP.NET Tutorial By Coder Baba www.youtube.com/coderbaba

You might also like