XML Data Source Controls and Data Binding

XML Data Source Controls and Data Binding

ASP.NET developers, and Web developers in general, have used XML in many ways to generate output in a Web page and have been well supplied with tools and components to help with this task. For example, in ASP.NET 1.x, you can use the Xml server control to display an XML document and can optionally apply an XSL or XSLT transformation to it first.

However, ASP.NET is changing the approach to connecting to data sources with the advent of a series of data source controls. The three that apply to XML are listed below.

  1. The XmlDataSource control can expose hierarchical XML documents and XML data for data binding to controls such as a TreeView, as well as exposing the data as an XmlDataDocument.

  2. The DataSetDataSource control exposes nonhierarchical XML as a rowset for data binding, as well as exposing the data as an ADO.NET DataSet and an XmlDataDocument.

  3. The WebServiceDataSource control allows you to work with data exposed from a Web Service.

We'll look briefly at these three controls in the next subsections. For more details on data source controls and their use in data binding with ASP.NET, refer to the companion book to this one, A First Look at ASP.NET v. 2.0 (Boston, MA: Addison-Wesley, 2004, ISBN 0-321-22896-0).

The XmlDataSource Control

To give you some idea of what the data source controls do, the following example uses an XmlDataSource control to load XML data and expose it for data binding. The XML is a list of "extra" packages for cars and has the format shown in Listing 7.20 (abbreviated to save space).

An XML List of "Extra" Packages for Well-Known Cars
  <Manufacturer Make="Audi">
    <Car Model="A4" Id="02347">
      <Package Trim="Sport"/>
      <Package Trim="Luxury"/>
      <Package Trim="Cold Weather"/>
    <Car Model="A6" Id="02932">
      ... packages ...
    <Car Model="A8"  Id="09381">
      ... packages ...
  <Manufacturer Make="BMW">
      ... cars and packages ...
  <Manufacturer Make="Porsche">
      ... cars and packages ...

A TreeView control is designed to display hierarchical data like this XML document and can be used to display it in an ASP.NET page without requiring any code to be written. An XmlDataSource control specifies the path to the XML document. The TreeView control is bound to the XmlDataSource control by specifying its id property value as the DataSourceID property of the TreeView control (see Listing 7.21).

Declaring a TreeView Control to Display the XML
<form runat="server">

<asp:XmlDataSource id="ds1" runat="server"
     DataFile="XmlDataSource.xml" />

<asp:TreeView id="tv1" DataSourceID="ds1" runat="server">
    <asp:TreeNodeBinding DataMember="Manufacturer" TextField="Make" />
    <asp:TreeNodeBinding DataMember="Car" TextField="Model" />
    <asp:TreeNodeBinding DataMember="Package" TextField="Trim" />


The XML document contains nested repeated elements, so the bindings must be declared or all that will be displayed are the element names. Within the TreeView element declaration are three bindings that specify that the control should bind to the Manufacturer element and expose the value of the Make attribute as the text of the nodes, then bind to the Car elements and expose the Model attribute of each one as the text of the nodes, and finally bind to the Package elements and display the value of the Trim attribute as the node values. The result can be seen in Figure.

9. Binding XML data to an ASP.NET TreeView control


Rather than specifying the XML as a file path and name for the DataFile property, the XML can be declared in-line in a <Data> element within the <asp:XmlDataSource> element. And you can also specify the schema and stylesheet as attributes in the declaration of the control or assign the values at runtime.

As well as all these features, the control supports caching to improve performance and can be used to update the XML disk file if required. Also note that an XPath can be specified to filter or select which of the elements in the XML source will be exposed. The outline declaration of the XmlDataSource control is shown in Listing 7.22.

The ASP.NET XmlDataSource Control Outline Declaration
<asp:XmlDataSource id="String" runat="server"
  OnTransforming="EventHandler" >

      [...In-line XML Data...]
      [...In-line XML Schema...]
      [...In-line XSL/XSLT Transform...]


The DataSetDataSource Control

The DataSetDataSource control is similar to the XmlDataSource control but works only with nonhierarchical XML data, that is, data that contains only one "level" of elements within the root element. The root element is exposed as the "table" name, and the nested elements as the columns of this "table," as in Listing 7.23.

Nonhierarchical XML Data for a DataSetDataSource Control

The DataSetDataSource control does not provide the XPath and UniqueID attributes. However, it has an extra method named GetDataSet that exposes the XML source data as a standard ADO.NET DataSet, allowing for runtime manipulation of the content in code.

The WebServiceDataSource Control

The WebServiceDataSource control can expose hierarchical data and is designed to interact with a Web Service and expose the XML content it returns. Three properties specific to this control specify how it connects to the Web Service: URL (a required String value), TypeName (an optional String value), and Timeout (an optional Integer value).

For more details, see the companion book mentioned earlier.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows