Represents an XML data source to data-bound controls.
Assembly: System.Web (in System.Web.dll)
[ToolboxBitmapAttribute(typeof(XmlDataSource))] [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
The control is a data source control that presents XML data to data-bound controls. The control can be used by data-bound controls to display both hierarchical and tabular data. The control is typically used to display hierarchical XML data in read-only scenarios. Because the control extends the HierarchicalDataSourceControl class, it works with hierarchical data. The control also implements the IDataSource interface and works with tabular, or list-style, data.
For security purposes, none of the 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 control to display XML data using data-bound controls.
The 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 control is commonly used in read-only data scenarios where a data-bound control displays XML data. However, you can also use the 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 control:
The Save method does not handle concurrent save operations by different requests. If more than one user is editing an XML file through the , 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 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.
The 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 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 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 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.
Not supported by the control.
The XPath property can be used to filter the XML data using an appropriate XPath expression.
Not supported by the control.
Because the 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 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 control retrieves an XmlHierarchicalDataSourceView for any unique hierarchical path passed to the GetHierarchicalView method.
|Walkthrough: Creating a Web Page to Display XML Data||Building ASP .NET Web Applications in Visual Studio|
|Walkthrough: Displaying and Tracking Advertisements with the AdRotator Control||Building ASP .NET Web Applications in Visual Studio|
|Walkthrough: Displaying an XML Document in a Web Forms Page Using Transformations||Building ASP .NET Web Applications in Visual Studio|
This section contains two code examples. The first code example demonstrates how to use an control with a TreeView control to display XML data from the sample XML file. The second example demonstrates how to use an control with a templated Repeater control to display XML data.
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="form1" runat="server"> <asp:xmldatasource id="XmlDataSource1" runat="server" datafile="books.xml" /> <!- TreeView uses hierachical data, so the XmlDataSource uses an XmlHierarchicalDataSourceView when a TreeView is bound to it. --> <asp:TreeView id="TreeView1" runat="server" datasourceid="XmlDataSource1"> <databindings> <asp:treenodebinding datamember="book" textfield="title"/> </databindings> </asp:TreeView> </form> </body> </html>
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 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 represents. For more information about XPath and XPathSelect data-binding syntax, see the XPathBinder class.
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Order</title> </head> <body> <form id="form1" runat="server"> <asp:XmlDataSource runat="server" id="XmlDataSource1" XPath="orders/order" DataFile="order.xml" /> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1"> <ItemTemplate> <h2>Order</h2> <table> <tr> <td>Customer</td> <td><%#XPath("customer/@id")%></td> <td><%#XPath("customername/firstn")%></td> <td><%#XPath("customername/lastn")%></td> </tr> <tr> <td>Ship To</td> <td><%#XPath("shipaddress/address1")%></font></td> <td><%#XPath("shipaddress/city")%></td> <td><%#XPath("shipaddress/state")%>, <%#XPath("shipaddress/zip")%></td> </tr> </table> <h3>Order Summary</h3> <asp:Repeater ID="Repeater2" DataSource='<%#XPathSelect("summary/item")%>' runat="server"> <ItemTemplate> <b><%#XPath("@dept")%></b> - <%#XPath(".")%><br /> </ItemTemplate> </asp:Repeater> <hr /> </ItemTemplate> </asp:Repeater> </form> </body> </html>
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>
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.