Section 4.9. Connect to an XML Document

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.

  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></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></docs>         <generator>Weblog Editor 2.0</generator>         <managingEditor></managingEditor>         <webMaster></webMaster>         <item>             <title>Star City</title>             <link></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=" gctc_e.htm"&gt;Star City&lt;/a&gt;.</description>             <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>             <guid></guid>         </item>         <item>             <title>The Engine That Does More</title>             <link></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></guid>         </item>         <item>             <title>Astronauts' Dirty Laundry</title>             <link></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></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).

    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.

    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.

    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  runat="server" DataSource>    <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.

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.

  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.

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

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

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

ASP. NET 2.0(c) A Developer's Notebook 2005
ASP. NET 2.0(c) A Developer's Notebook 2005
Year: 2005
Pages: 104 © 2008-2017.
If you may any questions please contact us: