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.
<?xml version='1.0'?>
<!-- This file represents a fragment of Product
store inventory database -->
<Products>
<Product ID="1" Name="Chai"
Price="13">
<Desc Value="10 boxes x 20 Bags"/>
</Product>
<Product ID="2" Name="Chang"
Price="10">
<Desc Value="24 -12 oz bottles"/>
</Product>
<Product ID="3" Name="Tofu"
Price="23.25">
<Desc Value="40-100 kg pkgs"/>
</Product>
</Products>
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" >
<head runat="server">
<title>XML Data Binding with a TreeView Control
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" Runat="server"
DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBinding
DataMember="Product"
ValueField="ID"
TextField="Name">
</asp:TreeNodeBinding>
<asp:TreeNodeBinding
DataMember="Desc"
ValueField="Value"
TextField="Value">
</asp:TreeNodeBinding>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource
ID="XmlDataSource1"
Runat="server"
DataFile="~/Data/ProductDetails.xml">
</asp:XmlDataSource>
</div>
</form>
</body>
</html>
 | |
| 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,
ValueField, 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.