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 GridViewand 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
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.
<!-- This file represents a fragment of Product
store inventory database -->
<Product ID="1" Name="Chai"
<Desc Value="10 boxes x 20 Bags"/>
<Product ID="2" Name="Chang"
<Desc Value="24 -12 oz bottles"/>
<Product ID="3" Name="Tofu"
<Desc Value="40-100 kg pkgs"/>
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"%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<title>XML Data Binding with a TreeView Control
<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" Runat="server"
|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 <asp:TreeView>
elements associate properties of individual TreeNode objects (such as DataMember
, and TextField
) to attributes of XML nodes in the hierarchy. When you perform data binding, attributes in the XML elements are promoted to properties of the data item. Also note that the TreeView hierarchy exactly matches the hierarchy of the source XML; if the source XML doesn't follow the same hierarchy, you would not get desirable results. Because of this, you would normally construct the XML specifically for binding to the TreeView.
When you execute the page, it will look similar to Figure 1