The Xml DataSource Server Control

ASP.NET contains a series of data source controls designed to bridge the gap between your data stores (such as XML) and the data-bound controls at your disposal. These new data controls not only enable you to retrieve data from various data stores, they also enable you to easily manipulate the data (using paging, sorting, editing, and filtering) before the data is bound to an ASP.NET server control.

With XML being as important as it is, a specific data source control is available in ASP.NET just for retrieving and working with XML data: XmlDataSource. This control enables you to connect to your XML data and use this data with any of the ASP.NET data-bound controls. Just like the SqlDataSource and the ObjectDataSource controls, the XmlDataSource control enables you to not only retrieve data, but also insert, delete, and update data items. With increasing numbers of users turning to XML data formats, such as Web services, RSS feeds, and more, this control is a valuable resource for your Web applications.

To show the XmlDataSource control in action, first create a simple XML file and include this file in your application. The following code reflects a simple XML file of Russian painters:

<?xml version="1.0" encoding="utf-8" ?> <Artists>

<Painter name="Vasily Kandinsky"> <Painting>

<Title>Composition No. 218</Title> <Year>1919</Year> </Painting> </Painter>

<Painter name="Pavel Filonov"> <Painting>

<Title>Formula of Spring</Title> <Year>192 9</Year> </Painting> </Painter>

<Painter name="Pyotr Konchalovsky"> <Painting>

<Title>Sorrento Garden</Title> <Year>1924</Year> </Painting> </Painter> </Artists>

Now that the Painters.xml file is in place, the next step is to use an ASP.NET DataList control and connect this DataList control to an <asp:XmlDataSource> control, as shown here:

<%@ Page Language="vb" AutoEventWireup="false"

CodeBehind="Default.aspx.vb" Inherits="XmlWeb._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=" 9/xhtml"> <head runat="server">

<title>Using XmlDataSource</title> </head> <body>

<form id="form1" runat="server"> <div>

<asp:DataList ID="PainterList" runat="server" DataSourceID="PainterData">

<ItemTemplate> <p>

<%# XPath("@name") %></b><br />

<%# XPath("Painting/Title") %></i><br /> <%# XPath("Painting/Year") %></p> </ItemTemplate> </asp:DataList>

<asp:XmlDataSource ID="PainterData" runat="server"

DataFile="~/Painters.xml" XPath="Artists/Painter" />

</div> </form> </body> </html>

Code snippet from XmlWeb

This is a simple example, but it shows you the power and ease of using the XmlDataSource control. Pay attention to two attributes in this example. The first is the DataFile attribute. This attribute points to the location of the XML file. Because the file resides in the root directory of the Web application, it is simply -/Painters.xml. The next attribute included in the XmlDataSource control is the XPath attribute. The XmlDataSource control uses XPath for the filtering of XML data. In this case, the XmlDataSource control is taking everything within the <Painter> set of elements. The value Artists/ Painter means that the XmlDataSource control navigates to the <Artists> element and then to the <Painter> element within the specified XML file.

The DataList control next must specify the DataSourceID as the XmlDataSource control. In the <ItemTemplate> section of the DataList control, you can retrieve specific values from the XML file by using XPath commands. The XPath commands filter the data from the XML file. The first value retrieved is an element attribute (name) contained in the <Painter> element. When you retrieve an attribute of an element, you preface the name of the attribute with an @ symbol. In this case, you simply specify @name to get the painter's name. The next two XPath commands go deeper into the XML file, getting the specific painting and the year of the painting. Remember to separate nodes with a /. When run in the browser, this code produces the results shown in Figure 9-3.

Besides working from static XML files such as the Painters.xml file, the XmlDataSource file can work from dynamic, URL-accessible XML files. One popular XML format pervasive on the Internet today is blogs, or weblogs. Blogs can be viewed either in the browser (see Figure 9-4), through an RSS aggregator, or just as pure XML.

Uiinq XmlDataSource

Composition No. 218 1919

Formuln of Spring 1929

Sorrento Garden 1924

FiGuRE 9-3

FiGuRE 9-3

3? Saul 5hanff t Blae for the R

World by | [pshcnH

1 hour, 1 minutes ago

AS P. MET Weblogs

Usirm P

Willi rtvm

Surne pr'jyidiimnny ntudtiuni. "squire yuu lu utre D-yndinu. SQL Of luurbe the (jrublern witli UiHiy DyiiainrL SQL ib Llidt llirs cviii ItiruJ lu SQI Injt*; linn rtllrtiJt.v Huwrvijr, ytm oiij liviuil IIiknk (irublrnis, liy jusl clutnyirMj liuw yuu sulirriil nyiirtiiiit Sijl tu yntir bnric nnrl...

Filed under: SOL. .NET. Imcction Attack fcstedtoi Active August

# Microaofe and □•accmitA.cp.N'CT Active August...Summary l-or those who were not aware Active Auaust was mostly S(JL Server LSBA'c and many others who nare Twitter friends and comrades. Twiner trend/search : «"ActiveAugust YouH see many of the Active August participants

byI_I Filled iiimIc,: niwiiiinlASP.NFT Mir riiMift ASP.NFT Wras, Tins, r<imiiiiiuitv Nrw.v

1 hour. ID minutes sed

Pasted to;

3P fianv Pillflnnrnn

Viliram i-airhotu by | | vikiUUUUin

Corootex Tyres in the Entity FramcworK

In this post I will describe the process you need to oo through to get o stored procedure to return a complex type in the fcntity J-rameviorlc It is very easy in the fcntity l-ramework to quickly create crud functions that will manipulate data in your tables...

Filed under: ,NET, asp.nei, MVC, Entity Framework

Pruunmnrniltcaltv Create PPF frurn Ciyslal Repuit Hir 5-juis time* when ivurkmy witli Liy^ldl lepurl vi yrm wrinl Id mi Ihr rr|imt in PDF nr rilhf r fiitrr

Tiled under; ASP.HCT

1 yuu du irjt wdiil tu StIiüfV the il willitiul iniuuiutj uvrr !u itu t*i rdw Turiridl bul m^. i Iw rlnutf Hrtsaly in

Posted to i if I5giialrjjLilg * P.UV Oahgiuvg'j

My Videos

I've started to load up all the videos of me from conferences and such on the web, and put them al in one placc. I hey are all over here . for example! ueep Reflection - from lechtd IUU - Understanding Hock Obiects Filed under: Agile figure 9-4

Now that you know the location of the XML from the blog, you can use this XML with the XmlDataSource control and display some of the results in a DataList control. The code for this example is shown here:

<%@ Page Language="vb" AutoEventWireup="false"

CodeBehind="Default.aspx.vb" Inherits="ViewingRss._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head runat="server">

<title>Viewing RSS</title> </head> <body>

<form id="form1" runat="server"> <div>

<asp:DataList ID="RssList" runat="server" DataSourceID="RssData"> <HeaderTemplate>

<table border="1" cellpadding="3"> </HeaderTemplate> <ItemTemplate> <tr>

<%# XPath("title") %></b><br />

<%# "published on " + XPath("pubDate") %></i><br /> <%# XPath("description").ToString().Substring(0,100) %> </td> </tr> </ItemTemplate> <AlternatingItemTemplate>

<tr style="background-color: #e0e0e0;"> <td>

<%# XPath("title") %></b><br />

<%# "published on " + XPath("pubDate") %></i><br /> <%# XPath("description").ToString().Substring(0,100) %>

372 | chapter 9 using xml with visual basic

</AlternatingItemTemplate> <FooterTemplate>

</table> </FooterTemplate> </asp:DataList>

<asp:XmlDataSource ID="RssData" runat="server"

DataFile="" XPath="rss/channel/item" />

</div> </form> </body> </html>

Code snippet from ViewingRSS

This example shows that the DataFile points to a URL where the XML is retrieved. The XPath property filters out all the <item> elements from the RSS feed. The DataList control creates an HTML table and pulls out specific data elements from the RSS feed, such as the <title>, <pubDate>, and <description> elements. To make things a little more visible, only the first 100 characters of each post are displayed.

Running this page in the browser results in something similar to what is shown in Figure 9-5.

Vlewl ng R 5 S - Windows Internei Explorer I m II ^ ^Ivl

Vlewl ng R 5 S - Windows Internei Explorer I m II ^ ^Ivl

•¿if Favorites Suuuested Sit« T ^ Gel More Add-uus T

^VicwinqRSS ^ ' 0 'y f ' Page** Safety ^ Tools

Using Parameters with Dynamic SQL

published on Mon, 31 Aug 2009 17:08:00 GMT

Some programming situations require you to use Dynamic SQL. Of course the problem

Arlit f August published on Mon, 31 Aug ?00Q 16:50:00 GMT

Active August... Summary' For those who were not aware Active August was mosdy SQL Server DBA s and man

Complex Types in the Entity Framework published on Mon, 31 Aug 2009 15:51:00 GMT

In this post I will describe the process you need to go through to get a stored procedure to retu

<ir:i(Tilif'\I5i Ironlilnv Microsoft SQL Scrvfr ;tml mv lilog fulurn.

published on Mon, 31 Aug 2009 14:54:32 GMT

Winston Churchill said - "Democracy is the worst form of government except for all those others that

Programmatic ally Create PDF from C rystal Report published on Mon, 3lAttx 2009 12:42:00 GMT Hi,

Some times when working with crystal r

published on Mon, 31 Aug 2009 09:00:13 GMT

I ve started to load up all the videos of me from conferences and such on the web, and put them a

Mixing Silverlight and MS ASP..\ET AJAX 3.5 in tbe same web application.

published on Mon, 31 Aug 2009 02:45:00 GMT

With the templates coming out of VS200S when you create a Stlverfight application, a ASP.NET Web


Dona ^ Local intranet | Protected Mode: OR ^ ▼ ^ioo% ▼

This approach also works with XML Web Services, even those for which you can pass in parameters using http-get. You just set up the DataFile value in the following manner:

DataFile=" 01"

0 0

Post a comment