O'Reilly logo

ASP.NET 2.0: A Developer's Notebook by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

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.

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

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

  3. 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.htm"&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/03.html#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/27.html#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/20.html#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:

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

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

    Creating a new data source for the DataList control

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

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

    Choosing the XML File data source

    Figure 4-64. Choosing the XML File data source

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

    Loading the XmlDataSource control with an XML document

    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.

  8. 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>
  9. Apply the Sand & Sky theme to the DataList control using Auto Format... in the DataList Tasks menu.

  10. 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.

Displaying a weblog without writing code

Figure 4-66. Displaying a weblog without writing code

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.

  1. 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.

    Adding the TextBox and Button controls to the form

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

  2. 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
  3. 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).

Feeding an XML document dynamically to the XmlDataSource control

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

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_intro.html.

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required