ASP.NET 2.0: A Developeramp;#039;s Notebook [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

ASP.NET 2.0: A Developeramp;#039;s Notebook [Electronic resources] - نسخه متنی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید


4.9. Connect to an XML Document

Note: Use an XML document easily through the XmlDataSource
control. No more lengthy code to write to manipulate XML
documents.

Like the SqlDataSource control, the new
XmlDataSource control allows you to deal with XML documents without
having to write a large amount of code. It is most suited for data
binding to controls such as the DataList and TreeView controls.


4.9.1. How do I do that?

In this lab, you will create an RSS reader that
consumes
an RSS document, and then use the DataList and the XmlDataSource
controls to display the news in a human-readable form.

Launch Visual Studio 2005 and create a new web site project. Name the
project C:\ASPNET20\chap04-XMLDataSource.

Right-click the project name in Solution Explorer and select Add New
Item.... Select XML File and name it RSS.xml.

Populate the RSS.xml document as shown in Example 4-11.


Example 4-11. A sample RSS document

<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Weblog Editor 2.0</generator>
<managingEditor>editor@example.com</managingEditor>
<webMaster>webmaster@example.com</webMaster>
<item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description>How do Americans get ready to work with Russians
aboard the International Space Station? They take a crash course in culture,
language and protocol at Russia's &lt;a href="http://howe.iki.rssi.ru/GCTC/
gctc_e"&gt;Star City&lt;/a&gt;.</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/06/03l#item573</guid>
</item>
<item>
<title>The Engine That Does More</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
<description>Before man travels to Mars, NASA hopes to design new
engines that will let us fly through the Solar System more quickly. The proposed
VASIMR engine would do that.</description>
<pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/27l#item571</guid>
</item>
<item>
<title>Astronauts' Dirty Laundry</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
<description>Compared to earlier spacecraft, the International
Space Station has many luxuries, but laundry facilities are not one of them. Instead,
astronauts have other options.</description>
<pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/20l#item570</guid>
</item>
</channel>
</rss>

Tip: You can obtain your own RSS documents from the Web as well. Here are
some places for you to get sample RSS documents:

http://www.cnet.com/4520-6022_1-5115040-1l?tag=cnetfd.ft

http://msdn.microsoft.com/aboutmsdn/rss/

Drag and drop the DataList control (located in the Toolbox under the
Data tab) onto the default form.

In the DataList Tasks menu of the DataList control, select
<New data source . . . > (see Figure 4-63).


Figure 4-63. Creating a new data source for the DataList control

Select XML File (see Figure 4-64). Use the default
XmlDataSource1 as the ID for the XmlDataSource
control. Click OK.


Figure 4-64. Choosing the XML File data source

Enter RSS.xml for the datafile (see Figure 4-65) and rss/channel/item for
the XPath Expression. Click OK.


Figure 4-65. Loading the XmlDataSource control with an XML document

Tip: The XPath Expression specifies the subset of the XML file that you
are interested in. In this case, you are only interested in the
<item> elements, so you specify the XPath expression as
rss/channel/item.

Switch to Source View for the default Web Form and add in the
following code (in bold) to configure the DataList control so that it
will display the appropriate sections of the XML file:

<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1">
<itemtemplate>
<b>#XPath("title")%> </b><br />
<i>#XPath("description") %></i>&nbsp;#XPath("pubDate")%><br />
<a href='#XPath("link") %>'>Link</a><br />
<br />
</itemtemplate>
</asp:DataList>

Apply the Sand & Sky theme to the DataList control using Auto
Format... in the DataList Tasks menu.

To test the application, press F5, and you now see the XML document
formatted as shown in Figure 4-66. Clicking on Link
will bring you to the source of the news.


Figure 4-66. Displaying a weblog without writing code


4.9.2. What about...

...loading the RSS document from the Web?

Besides loading an XML document from your local storage, you can also
load an XML document from the Web via the DataFile property.

Using the same project created in this section, add a TextBox and
Button control to the default Web Form. Name them as shown in Figure 4-67.


Figure 4-67. Adding the TextBox and Button controls to the form

In the click event of the Load RSS button, set the DataFile property
of the XmlDataSource control to the content of the text box:

Protected Sub btnLoadRSS_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnLoadRSS.Click
XmlDataSource1.DataFile = txtRssURL.Text
End Sub

To test the application, press F5, enter a URL pointing to an RSS
feed, and click Load RSS. The RSS feed should be retrieved and
displayed in the DataList control (see Figure 4-68).


Figure 4-68. Feeding an XML document dynamically to the XmlDataSource control


4.9.3. Where can I learn more?

If you are new to the world of RSS and weblogs, check out my
introductory article to weblogs at http://www.oreillynet.com/pub/a/javascript/2002/12/30/weblog_introl.

For more information on the XmlDataSource and DataList controls,
check out the MSDN Help topics "XmlDataSource
Members" and "DataList
Members."

/ 102