The XmlDataSource control is a data source control that presents XML data to data-bound controls. The XmlDataSource control can be used by data-bound controls to display both hierarchical and tabular data. The XmlDataSource control is typically used to display hierarchical XML data in read-only scenarios. Because the XmlDataSource control extends the HierarchicalDataSourceControl class, it works with hierarchical data. The XmlDataSource control also implements the IDataSource interface and works with tabular, or list-style, data.
Page developers use the XmlDataSource control to display XML data using data-bound controls.
The XmlDataSource typically loads XML data from an XML file, which is specified by the DataFile property. XML data can also be stored directly by the data source control in string form using the Data property. 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 transformation. As with the XML data, you typically load the style sheet from a file, indicated by the TransformFile property, but you can also store it in string form directly using the Transform property.
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 control to edit XML data. To edit the XML data, call the GetXmlDocument method to retrieve an XmlDataDocument object that is an in-memory representation of the XML data. You can use the object model exposed by the XmlDataDocument and XmlNode objects it contains or use an XPath filtering expression to manipulate data in the document. When you have made changes to the in-memory representation of the XML data, you can save it to disk by calling the Save method.
There are some restrictions to the editing capabilities of the XmlDataSource control:
· The XML data must be loaded from an XML file that is indicated by the DataFile property, not from inline 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 through the XmlDataSource, there is no guarantee that all users are operating with the same data. It is also possible for a Save operation to fail due to these same concurrency issues.
A common operation performed with XML data is transforming it from one XML data set into another. The XmlDataSource control supports XML transformations with the Transform and TransformFile properties, which specify an XSL style sheet to apply to XML data before it is passed to a data-bound control, and the TransformArgumentList property, which enables you to supply dynamic XSLT style sheet arguments to be used by an XSL style sheet during the transformation. If you specify an XPath filtering expression using the XPath property, it is applied after the transformation takes place.
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 expression. The XPath property supports an XPath-syntax filter that is applied after XML data is loaded and transformed.
For performance purposes, caching is enabled for the XmlDataSource control by default. Opening and reading an XML file on the server every time a page requested can reduce the performance of your application. Caching lets you reduce the processing load on your server at the expense of memory on the Web server; in most cases this is a good trade-off. The XmlDataSource automatically caches data when the EnableCaching property is set to true, and the CacheDuration property is set to the number of seconds that the cache stores data before the cache is invalidated. You can use the CacheExpirationPolicy to further fine-tune the caching behavior of the data source control.
Capability | Description |
Sorting | Not supported by the XmlDataSource control. |
Filtering | The XPath property can be used to filter the XML data using an appropriate XPath expression. |
Paging | Not supported by the XmlDataSource control. |
Updating | Supported by manipulating the XmlDataDocument directly and then calling the Save method. |
Deleting | Supported by manipulating the XmlDataDocument directly and then calling the Save method. |
Inserting | Supported by manipulating the XmlDataDocument directly and then calling the Save method. |
Caching | Enabled by default, with the CacheDuration property set to 0 (infinite) and the CacheExpirationPolicy property set to Absolute. |
Because the XmlDataSource control supports data-bound controls that display hierarchical data as well as controls that display tabular data, the data source control supports multiple types of data source view objects on its underlying XML data. The XmlDataSource control retrieves a single named XmlDataSourceView object when used with a data-bound control that displays tabular data. The GetViewNames method identifies this single named view. When used with a data-bound control that displays hierarchical data, the XmlDataSource control retrieves an XmlHierarchicalDataSourceView for any unique hierarchical path passed to the GetHierarchicalView method.
Iana Tsolova is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.