ML data is generally used to represent semi-structured or hierarchal data. Using XML documents as your data source allows you to receive XML documents from other sources and format the XML data to be compatible with your application. ASP.NET 1.0 let you use XML data sources, but much of that work required manual coding to get the data into a format suitable for display. In contrast, ASP.NET 2.0 provides a new XmlDataSource control that simplifies binding XML data to controls such as the TreeView and GridView?and it works with both hierarchical and tabular data.
Loading the XmlDataSource Control
Typically, you’d use the XmlDataSource control to display hierarchical XML data in read-only scenarios. However, because the control implements the IDataSource interface, it also works with tabular, or list-style, data. You can load the XmlDataSource control from either an XML file, by setting the DataFile property, or from a string containing XML, by setting the control’s Data property to the string.
While loading XML data, you can also specify an XML schema. The XmlDataSource control doesn’t use the schema, but data-bound controls can access it if required. To load a schema, you set the SchemaFile property value to the appropriate XML schema file. Alternatively, you can also store a schema in string form directly from code using the Schema property.
This article shows examples of how you might use the XmlDataSource control to display data in hierarchical and tabular data formats. To begin with, here’s a simple example that displays XML data from an XmlDataSource control in a TreeView control.
Binding the XmlDataSource Control to a TreeView Control
To begin, create the XML file shown below and save it as ProductDetails.xml. I’ll use that file as a data source throughout this article. The file contains a fragment of information on the various products in the Products table in the Northwind database.
After creating the XML file, create a new ASP.NET page to consume it and display its information in a TreeView control. For example, the following page code shows a binding example using an XmlDataSource control and a TreeView control.
<%@ Page Language="VB"%>
XML Data Binding with a TreeView Control
|Figure 1. TreeView Binding Example Page: Output from the XmlDataSource control is data-bound to a TreeView Control, which gets the data from the ProductDetails.xml file.|
Note that the
When you execute the page, it will look similar to Figure 1.
XPath Data Binding Syntax
By default, the XmlDataSource control loads all the XML data in the XML file identified by the DataFile property or found inline in the Data property, but you can filter the data using an XPath query. The XPath property supports an XPath-syntax filter that the XmlDataSource control applies after it loads and transforms the XML data. The control supports the XPath-based data binding syntax on any data item that implements the IXPathNavigable interface. The syntax of the XPath expression is:
The property evaluates an XPath expression against the data item and returns a single value.
The following code example uses XPath data binding expressions to bind to attributes of the Product nodes. This particular example simply uses the ‘@’ prefix?the XPath syntax for a node attribute?to retrieve the attribute value in each expression. However, you can also use the XPath expression to refer to arbitrary items within the hierarchy.
<%@Page Language="VB" Debug="True" %>
XPath Select Method ExampleXPath Select with a FormView Control
|Figure 2. The XPath Example ASP.NET Page. The figure shows the result of data-binding a FormView control to the XmlDataSource control.|
The ASP page described in the preceding code binds a FormView control to the XmlDataSource control. FormView is a new control supplied with ASP.NET 2.0, which can be directly associated with a DataSource control. After making the initial association, the FormView control, in conjunction with the DataSource control, handles Insert, Delete, Update, and Get operations. The example creates an ItemTemplate element inside the FormView control and specifies the XPath expression as part of the Label declarations. Figure 2 shows how the results look in a browser.
Paging is enabled in the screen shown in Figure 2, and users can click on the page numbers to navigate to the details of different products.
Performing XSL Transformations with the XmlDataSource Control
You can also transform the XML data before displaying it in a data-bound control by providing an Extensible Stylesheet Language (XSL) stylesheet for the transformation. You typically use the TransformFile property, which accepts a file name, to load the stylesheet from a file, but you can also load a stylesheet directly from a string using the Transform property.
Here’s an example XSL stylesheet that demonstrates how to apply an XSL Stylesheet file to the contents of the XmlDataSource control to transform that XML format into a different XML form before displaying the data in a TreeView control.
The XSL file transforms the ProductDetails.xml file into a different format?changing the names of the XML elements and the case of the attributes in the output. The next example shows how to apply the ProductDetails.xsl file to the ProductDetails.xml file, using a TreeView control to display the transformed XML content.
<%@ Page Language="VB" %>
|Figure 3. The Transformed XPath Example ASP.NET Page: The page shown in this figure was created by using an XSL Stylesheet file in conjunction with an XmlDataSource control to transform one XML format into another XML format, binding the result to the TreeView.|
As part of the XmlDataSource control declaration, the DataFile and TransformFile attributes (properties) are set to ProductDetails.xml and ProductDetails.xsl respectively. After setting the attributes you can easily display the data in a TreeView control by setting the DataSourceID property of the TreeView control to the XmlDataSource control. Inside the TreeView declaration, you use the asp:TreeNodeBinding elements to specify which nodes and child nodes to display in the TreeView control.
Figure 3 shows the output produced by the page. Note that after the transformation, the top level node is products-list rather than Products as in the original ProductDetails.xml file. Apart from that, the data displayed is unchanged.
Displaying Hierarchical Data in a GridView Control
So far, you’ve seen how to display hierarchical information in a TreeView control and a FormView control. This section will provide you with an example on how to bind the XmlDataSource control to a DropDownList control and a GridView control. The example code uses the same ProductDetails.xml file as the XML source that you’ve already seen. As in the previous examples, add an XmlDataSource control to the ASP.NET page, but set the XPath attribute of the XmlDataSource control to Products/Product. This will filter for all the Product elements contained inside the root Products element.
Now, add a DropDownList and a GridView control to the page and set the DataSourceID attributes for both to the name of your XmlDataSource control (XmlDataSource1 by default). In the SelectedIndexChanged event of the XmlDataSource control, add code to set the XPath property of the XmlDataSource control to the value selected by the user in the DropDownList control. Finally, add a button to the ASP.NET page and inside the Click event of the button, add code to reset the XPath property of the XmlDataSource control to its original value (which is Products/Product). After making all of the changes, the code should look similar to Listing 1.
The code shown in Listing 1 uses the SelectedIndexChanged event of the DropDownList control to dynamically set the XPath property of the XmlDataSource control to the expression:
XmlDataSource1.XPath = "Products/Product[@ID=" + _ DropDownList1.SelectedValue + "]"
In other words the XPath expression uses the value selected by the user in the DropDownList to obtain the Product element with that ID attribute value. In the button’s Click event, the code resets the XPath property to the default value, which is also set in the XPath attribute as part of the XmlDataSource control declaration.
Executing the code in Listing 1 results in a page where, when users select a product in the DropDownList, the output displayed by the GridView varies accordingly (see Figure 4).
|Figure 4. A Bound GridView Control: When a user selects an item in the DropDownList, the page displays associated items in the GridView based on the ID attribute of the item selected.|
Caching an XmlDataSource Control
For performance purposes, caching is enabled for the XmlDataSource control by default. You typically use the XmlDataSource with an XML file?and opening and reading an XML file on the server every time a page is requested can have a negative impact on the performance of your application. Caching reduces the processing load on your servers at the expense of memory; but in most cases this is a good trade-off. To cache data in an XmlDataSource control, set the EnableCaching property to true. You also need to set the CacheDuration property to the number of seconds that the cache stores data before refreshing the cache. You can also use the CacheExpirationPolicy to gain a finer level of control over the caching behavior.
This article showed you how to take advantage of the XmlDataSource control in an ASP.NET page to connect to XML data sources and display that information in data-bound controls such as a TreeView, FormView, GridView or DropDownList. Using the XmlDataSource control, you can apply an external XSL file to transform the contents of the XML file into another format before displaying that information in a data-bound control, and you can use XPath queries to extract the data.