Export (0) Print
Expand All

XmlDataSource Class

Represents an XML data source to data-bound controls.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public class XmlDataSource extends HierarchicalDataSourceControl implements IDataSource, IListSource
<asp:XmlDataSource />

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.

NoteNote:

For security purposes, none of the XmlDataSource control properties are stored in view state. Since it is technically possible to decode the contents of view state on the client, storing sensitive information about the data structure or its contents could expose you to an information disclosure threat. Note that if you need to store information such as XPath property in view state, you can enable encryption to protect the contents by setting ViewStateEncryptionMode on the @ Page directive.

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.

NoteNote:

The XmlDataSource class uses the deprecated XslTransform class to perform XSL transformations. If you want to use style sheet features that were introduced after the XslTransform class was deprecated, apply the transforms manually by using the XslCompiledTransform class.

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.

TopicLocation
Walkthrough: Creating a Web Page to Display XML DataBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Displaying and Tracking Advertisements with the AdRotator ControlBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Displaying an XML Document in a Web Forms Page Using TransformationsBuilding ASP .NET Web Applications in Visual Studio

This section contains two code examples. The first code example demonstrates how to use an XmlDataSource control with a TreeView control to display XML data from the sample XML file. The second example demonstrates how to use an XmlDataSource control with a templated Repeater control to display XML data.

The following code example demonstrates how to use an XmlDataSource control with a TreeView control to display XML data. The XmlDataSource loads XML data from the XML file identified by the DataFile property.

No code example is currently available or this language may not be supported.

The XML file in the code example has the following data:

<books>
   <computerbooks>
     <book title="Secrets of Silicon Valley" author="Sheryl Hunter"/>
     <book title="Straight Talk About Computers" author="Dean Straight"/>
     <book title="You Can Combat Computer Stress!" author="Marjorie Green"/>                
   </computerbooks>
   <cookbooks>
     <book title="Silicon Valley Gastronomic Treats" author="Innes del Castill"/>
   </cookbooks>
</books>

The following code example demonstrates how to use an XmlDataSource control with a templated Repeater control to display XML data. The Repeater control uses an XPath data-binding expression to bind to data items within the XML document that the XmlDataSource represents. For more information about XPath and XPathSelect data-binding syntax, see the XPathBinder class.

No code example is currently available or this language may not be supported.

The XML file in the code example has the following data:

<?xml version="1.0" encoding="iso-8859-1"?>
 <orders>
   <order>
     <customer id="12345" />
     <customername>
         <firstn>John</firstn>
         <lastn>Smith</lastn>
     </customername>
     <transaction id="12345" />
     <shipaddress>
         <address1>1234 Tenth Avenue</address1>
         <city>Bellevue</city>
         <state>Washington</state>
         <zip>98001</zip>
     </shipaddress>
     <summary>
         <item dept="tools">screwdriver</item>
         <item dept="tools">hammer</item>
         <item dept="plumbing">fixture</item>
     </summary>
   </order>
</orders>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

orc_cpub14

Added note about use of deprecated class for XSL transformation.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft