Accessing and Updating Data in ASP.NET: Retrieving XML Data with XmlDataSource ControlBy Scott Mitchell
With its new declarative data source controls and improved data Web controls, ASP.NET 2.0 has greatly simplified the process of working with data. In Part 2 of this article series, Accessing Database Data, we looked at how to use the SqlDataSource control to retrieve data from a relational database, which could then be bound to any data Web control (such as the GridView, DetailsView, FormView, DataList, Repeater, and so on). XML data can be accessed (and displayed) just as easily with the XmlDataSource control.
The XmlDataSource control makes accessing, filtering, and transforming XML data a simple, code-free process. Additionally,
XPathSelect() databinding methods added to ASP.NET 2.0 make displaying particular
XML values or binding entire XML nodesets just as easy. And the XML data accessed can be from a local file or automatically
downloaded from a specified URL.
In this article we will examine how to use the XmlDataSource control and the
databinding methods, displaying the results in a variety of data Web controls. Read on to learn more!
The XmlDataSource control exists merely as a proxy for retrieving XML data, which can then be programmatically accessed or bound to a data Web control. To access XML data from an ASP.NET page using the XmlDataSource control, start by dragging the control from the Toolbox onto the Designer (the XmlDataSource control is found in the Data section of the Toolbox). From the XmlDataSource's smart tag, click the "Configure Data Source" link to bring up the Configure Data Source wizard (shown below). From the wizard, you can set the XmlDataSource control's three most useful properties:
DataFile- specifies the file that contains the XML data to access. This can be a local file, like
~/App_Data/XMLFile.xml, or a remote file, like
TransformFile- XML documents can be transformed from one structure to another using a markup language known as XSL Transformations, or XSLT. If you want to transform the XML contents in
DataFilebefore working with the XML data, specify the XSLT file to perform the transformation here. For more information on XSLT, check out the XSLT Tutorial at W3 Schools.
XPath- XPath is an XML syntax used to filter the contents of an XML document, returning a particular value or nodeset. If you want to work with a particular set of XML data, specify an XPath expression here to limit the results. See W3 School's XPath tutorial for more information.
If you do not have the XML data or XSLT saved in a file, you can specify the raw XML or XSLT directly through the XmlDataSource's
These properties cannot be set through the Configure Data Source wizard, but instead must be set through the Properties window.
(Click on the XmlDataSource control in the Designer and hit F4.)
Binding the XmlDataSource Control to a Data Web Control and Accessing Particular XML Data
Once you've configured the XmlDataSource control, add a data Web control to the page and set its
DataSourceIDproperty to the
IDof the XmlDataSource control (a task that can easily be done through the drop-down list in the data Web control's smart tag). ASP.NET 2.0 offers two new databinding methods for accessing a particular value or noteset from the XML data bound to the data Web control:
XPath("xpath")- accesses a particular value. To retrieve the text value of a node, simply refer to the node in the xpath; for an attribute, prefix the attribute name with
XPathSelect("xpath")- returns a nodeset. Can be used to bind a set of nodes to a nested data Web control.
Example 1: Displaying a Remote RSS 2.0 Feed in a DataList
RSS, or Really Simple Syndication, is an XML format designed to allow website content to be easily syndicated. The syndicated content can be integrated into other websites, or can be viewed by individuals via an assortment of desktop applications. Many sites syndicate content today using RSS 2.0. For example, the latest 4Guys articles are syndicated at http://aspnet.4guysfromrolla.com/rss/rss.aspx.
An RSS syndication feed has a format similar to the following:
In short, an RSS feed is made up of a set of
<item> elements, each of which represents some recent, syndicated
piece of content. For more information on RSS and the benefits of syndicating content, check out
Syndicating Your Web Site's Content with RSS; also consult
the RSS 2.0 Specification.
For this example, we want to bind the set of content items to a DataList. Therefore, set the XmlDataSource control's
DataFile property to the URL of the remote RSS 2.0 feed source (such as
XPath property to
/rss/channel/item. This XPath statement instructs the XmlDataSource
to return the set of
<item> elements, meaning the DataList bound to this XmlDataSource will have one
record created for each
<item> element in the specified RSS feed.
Next, add the DataList and configure its
ItemTemplate to display the
<pubDate> of each content item, along with a link to read the content
<link> element's value). The following markup will work:
Note how the
<# XPath("xpath") %> syntax is used to grab a particular value from the current
node bound to the DataList. Since the DataList is bound to a set of
<%# XPath("link") %>
will return the value of the current
After specifying the
ItemTemplate, take a moment to view the page in a browser. The following screenshot shows
the output for the 4Guys RSS feed.
An even easier approach to displaying RSS feeds in a web page is to use my free, open-source RssFeed control! See A Custom ASP.NET Server Control for Displaying RSS Feeds for more information...
Example 2: Binding a Nodeset to a Nested Data Web Control
XPath("xpath")method allows us to bind a particular value relative from the current XML node, but what if we want to get back an entire nodeset and bind that to a nested data Web control? The
XPathSelect("xpath")method provides this functionality. To illustrate the use of this method, let's create an example that displays a receipt for a user's purchase, where the purchase details are spelled out in an XML format. In the download available at the end of this article you'll find a file named
PO.xmlin the project's
App_Datafolder. This XML file (whose primary structure was found online in this example XML file) contains information about a single order, and has the following structure:
To display a receipt, we need to display the customer's information, the subtotal, tax, and total, and the set of items
ordered. Since there can be a variable number of items, this can best be displayed by using a nested data Web control that's
bound to the set of
To display the customer and price details, add a FormView and bind it to a new XmlDataSource whose
~/App_Data/PO.xml. Do not bother setting the
XPath property. Next, in the FormView's
ItemTemplate, use the
XPath("xpath") method to display the the customer and price details:
Since the XmlDataSource control does not have an
XPath value specified, the
<Order> node is
what is bound to the FormView. Therefore, to get the
<Name> element value from the
node, we need to use the XPath statement
/Order/Customer/Name. To get an attribute value -
an attribute in the
<Order> element - prefix the attribute name with
The above databinding syntax displays the customer and price details in the FormView. We still need to display the set of
<Item>s associated with this order. To accomplish this, add a GridView control within the FormView's
ItemTemplate and assign its
DataSource property to the set of
<Item> nodes using the
<asp:GridView runat="server" ... DataSource='<# XPathSelect("/Order/Manifest/Item") %>' ...>.
Then, in the GridView's
<Columns> section, add a TemplateField for each of the item details to display:
ASP.NET 2.0 offers a number of different data source controls for working with different types of data. The SqlDataSource control, for example, is designed to work with data in a relational database. As we saw in this article, to work with XML data, use the XmlDataSource control. The XmlDataSource control can access local or remote XML files, or can have raw XML data assigned to its
Dataproperty. There's also the optional
XPathproperties that can be set to transform or filter the contents of the XML data.
Once the XmlDataSource control has been properly configured, it can be bound to a data Web control. The contents of the XML
returned by the XmlDataSource control can be retrieved as scalar values using
XPath("xpath") or as
XPathSelect("xpath"). See the download available at the end of this article for the
complete code explored in the two examples above.