Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

New Ways to Handle Data

Clicking the New hyperlink provides a table that enables you to enter a new customer after you add an INSERT command to the AccessDataSource control just as was done with the UPDATE command. This is shown in Figure 4-26.

Figure 4-26

Be sure that if you enable the inserting of data into your data store, that you define the <InsertParameters> using the <asp:Parameter> control — just as was done in the updating of the data.

XmlDataSource Control

So far, you have been looking at using the data source controls that work with traditional data stores such as Microsoft SQL Server and Microsoft Access. Today, however, a considerable amount of data is stored in XML format, so a specific data source control has been added to ASP.NET 2.0 just for retrieving and working with XML data.

The XmlDataSource control enables you to connect to your XML data and to use this data with any of the ASP.NET data-bound controls. Just like the SqlDataSource and the AccessDataSource controls, the XmlDataSource control also enables you to not only retrieve data, but also to insert, delete, and update data items. With the world turning more and more to XML data formats, such as Web services, RSS feeds, and more, this control is a valuable resource for your applications.

To show the XmlDataSource control in action, first create a simple XML file and include this file in your application. Listing 4-22 shows a simple XML file of Russian painters that we can use.

109

Chapter 4

Listing 4-22: Painters.xml

<?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>1929</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 a DataList control and connect this DataList control to an <asp:XmlDataSource> control. This is illustrated in Listing 4-23.

Listing 4-23: Using a DataList control to display XML content

<%@ Page Language=”VB”%>

<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>

<title>XmlDataSource</title>

</head>

<body>

<form id=”form1” runat=”server”>

<asp:DataList ID=”DataList1” Runat=”server” DataSourceID=”XmlDataSource1”> <ItemTemplate>

<p><b><%# XPath(“@name”) %></b><br />

<i><%# XPath(“Painting/Title”) %></i><br /> <%# XPath(“Painting/Year”) %></p>

</ItemTemplate>

</asp:DataList>

<asp:XmlDataSource ID=”XmlDataSource1” Runat=”server” DataFile=”~/Painters.xml” XPath=”Artists/Painter”> </asp:XmlDataSource>

</form>

</body>

</html>

110

New Ways to Handle Data

This is a simple example, but it shows you the power and ease of using the XmlDataSource control. You should pay attention to only 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 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) that is contained in the <Painter> element. If you are retrieving an attribute of an element, you preface the name of the attribute with an @ symbol. In this case then, you simply specify @name to get at the painter’s name. The next two XPath commands go deeper into the XML file and get 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 illustrated in Figure 4-27.

Figure 4-27

Besides working from static XML files like the Painters.xml file shown earlier, the XmlDataSource file has the capability to work from dynamic, URL-accessible XML files. One popular XML format that is pervasive on the Internet today is blogs or weblogs. Blogs, or personal diaries, can be viewed either in the browser, through an RSS-aggregator, or just as pure XML.

As you look at my blog in Figure 4-28, you can see the XML it produces directly in the browser. (You can find a lot of blogs to play with for this example at weblogs.asp.net.)

111

Chapter 4

Figure 4-28

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 in Listing 4-24.

Listing 4-24: Working with an RSS feed

<%@ Page Language=”VB”%>

<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>

<title>XmlDataSource</title>

</head>

<body>

<form id=”form1” runat=”server”>

<asp:DataList ID=”DataList1” Runat=”server” DataSourceID=”XmlDataSource1”> <HeaderTemplate>

<table border=”1” cellpadding=”3”> </HeaderTemplate>

<ItemTemplate>

<tr><td><b><%# XPath(“title”) %></b><br /> <i><%# XPath(“pubDate”) %></i><br />

<%# XPath(“description”) %></td></tr> </ItemTemplate> <AlternatingItemTemplate>

<tr bgcolor=”LightGrey”><td><b><%# XPath(“title”) %></b><br /> <i><%# XPath(“pubDate”) %></i><br />

<%# XPath(“description”) %></td></tr>

112

New Ways to Handle Data

</AlternatingItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:DataList>

<asp:XmlDataSource ID=”XmlDataSource1” Runat=”server” DataFile=”http://geekswithblogs.net/evjen/Rss.aspx” XPath=”rss/channel/item”>

</asp:XmlDataSource>

</form>

</body>

</html>

Looking at the code in Listing 4-24, you can see 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.

Running this page in the browser, you get something similar to the results shown in Figure 4-29.

Figure 4-29

113