XmlDataSource Control Overview

The XmlDataSource control makes XML data available to data-bound controls. You can use it to display both hierarchical and tabular data, although the XmlDataSource control is typically used to display hierarchical XML data in read-only scenarios.

Retrieving Data Using the XmlDataSource Control

The XmlDataSource loads XML data from an XML file specified using the DataFile property. XML data can also be loaded from a string using the Data property.

The XmlDataSource control exposes attributes of XML elements as data-bindable fields. If you want to bind to values that are not attributes, you can specify a transformation using an Extensible Sylesheet Language (XSL) style sheet. In control templates, such as in a FormView or GridView controls, you can also bind a control in a template to XML data using the XPath data-binding function. For information about using an XPath expression, see Binding a Tabular Control to the XmlDataSource Control.

The following code example shows an XmlDataSource and a TreeView control bound to it. The source XML is shown after the code example.

Transforming XML Data Using the XmlDataSource Control

If you want to transform the XML data before it is displayed by a data-bound control, you can provide an Extensible Sylesheet Language (XSL) style sheet for the XmlDataSource control. As with XML data, you typically load the style sheet from a file, which you specify using the TransformFile property. However, you can also load the style sheet directly from a string using the Transform property.

To supply dynamic XSLT style sheet arguments to be used by the XSL style sheet during the transformation, you can set the TransformArgumentList property.

Note

If you specify an XPath filtering expression using the XPath property, the filter is applied after the transformation takes place.

The following code example shows an XmlDataSource and a TreeView control bound to it. The sample uses the XML data from the preceding example. The XSL style sheet used by the example is shown after the code example.

Editing XML Data Using the XmlDataSource Control

The XmlDataSource control is commonly used in read-only data scenarios where a data-bound control displays XML data. However, you can also use the XmlDataSource to edit XML data. Note that automatic update, insert, and delete operations that work with other data source controls will not work. You must write custom code to modify data using the XmlDataSource control.

To edit the XML data, call the GetXmlDocument method to retrieve an XmlDocument object, which is an in-memory representation of the XML data. You can then use the object model exposed by the XmlDocument object and its XmlNode objects, or you can use an XPath expression to manipulate data in the document. When you have made changes to the in-memory representation of the XML data, you can save the data to disk by calling the Save method. This overwrites the XML file on disk in its entirety.

The following list indicates some restrictions on editing XML data using the XmlDataSource control:

The XML data must be loaded from an XML file specified with the DataFile property, and not from a string of XML specified in the Data property.

No XSLT transformation can be specified in the Transform or TransformFile properties.

The Save method does not handle concurrent save operations by different requests. If more than one user is editing an XML file using the XmlDataSource control, there is no guarantee that all users are operating with the same data, and no guarantee that one user will not overwrite changes from another user. It is also possible for a Save operation to fail because of another user is writing to the XML file and has an exclusive lock on the file.

Filtering XML Data using the XmlDataSource Control

By default, the XmlDataSource control exposes all of the XML data specified by the DataFile or Data properties. However, you can filter the data using an XPath expression. The XPath property allows you to specify an XPath filter expression that is applied after XML data is loaded and has had any transformations applied to it. For an example, see Filtering Data Using the XmlDataSource Control.

Caching XML Data using the XmlDataSource Control

The XmlDataSource control is typically used with an XML file, and opening and reading an XML file every time a page requested can affect the performance of your application. Therefore, caching is enabled for the XmlDataSource control by default. Caching lets you reduce the processing load on your server at the expense of memory on the Web server; in most cases this is an acceptable trade-off. The XmlDataSource control caches data when the EnableCaching property is set to true, which is the default. You set the CacheDuration property to the number of seconds that the control should cache data. You can use the CacheExpirationPolicy to fine-tune the caching behavior of the XmlDataSource control.

Important

It is recommended that you set the EnableCaching property to false when client impersonation is enabled and the source file for the XmlDataSource control is retrieved based on the client identity. If caching is enabled, cached XML data for a single user can be viewed by all users and sensitive information could be exposed to an unwanted source. Client impersonation is enabled when the impersonate attribute of the identity configuration element is set to true and anonymous identification is disabled for the application at the Web server.