The
Retrieving Data Using the XmlDataSource Control
The XmlDataSource loads XML data from an XML file specified using the
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
The following code example shows an XmlDataSource and a
Visual BasicВ | Copy Code |
---|---|
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <form runat="server"> <asp:XmlDataSource id="PeopleDataSource" runat="server" DataFile="~/App_Data/people.xml" /> <asp:TreeView id="PeopleTreeView" runat="server" DataSourceID="PeopleDataSource"> <DataBindings> <asp:TreeNodeBinding DataMember="LastName" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="FirstName" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Street" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="City" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Region" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="ZipCode" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Title" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Description" TextField="#InnerText" /> </DataBindings> </asp:TreeView> </form> </BODY> </HTML> |
C#В | Copy Code |
---|---|
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <form runat="server"> <asp:XmlDataSource id="PeopleDataSource" runat="server" DataFile="~/App_Data/people.xml" /> <asp:TreeView id="PeopleTreeView" runat="server" DataSourceID="PeopleDataSource"> <DataBindings> <asp:TreeNodeBinding DataMember="LastName" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="FirstName" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Street" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="City" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Region" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="ZipCode" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Title" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="Description" TextField="#InnerText" /> </DataBindings> </asp:TreeView> </form> </BODY> </HTML> |
The following shows the XML data used by the preceding code example.
В | Copy Code |
---|---|
<?xml version="1.0" encoding="utf-8" ?> <People> <Person> <Name> <FirstName>Manoj</FirstName> <LastName>Syamala</LastName> </Name> <Address> <Street>345 Maple St.</Street> <City>Redmond</City> <Region>WA</Region> <ZipCode>01434</ZipCode> </Address> <Job> <Title>CEO</Title> <Description>Develops company strategies.</Description> </Job> </Person> <Person> <Name> <FirstName>Jared</FirstName> <LastName>Stivers</LastName> </Name> <Address> <Street>123 Elm St.</Street> <City>Seattle</City> <Region>WA</Region> <ZipCode>11223</ZipCode> </Address> <Job> <Title>Attorney</Title> <Description>Reviews legal issues.</Description> </Job> </Person> <Person> <Name> <FirstName>Karina</FirstName> <LastName>Agerby</LastName> </Name> <Address> <Street>34 Palm Avenue</Street> <City>Renton</City> <Region>WA</Region> <ZipCode>63910</ZipCode> </Address> <Job> <Title>IT Director</Title> <Description>In charge of corporate network.</Description> </Job> </Person> </People> |
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
To supply dynamic XSLT style sheet arguments to be used by the XSL style sheet during the transformation, you can set the
Note |
---|
If you specify an XPath filtering expression using the |
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.
Visual BasicВ | Copy Code |
---|---|
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <form runat="server"> <asp:XmlDataSource id="PeopleDataSource" runat="server" TransformFile="~/App_Data/names.xsl" DataFile="~/App_Data/people.xml" /> <asp:TreeView id="PeopleTreeView" runat="server" DataSourceID="PeopleDataSource"> <DataBindings> <asp:TreeNodeBinding DataMember="Name" TextField="#InnerText" /> </DataBindings> </asp:TreeView> </form> </BODY> </HTML> |
C#В | Copy Code |
---|---|
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <BODY> <form runat="server"> <asp:XmlDataSource id="PeopleDataSource" runat="server" TransformFile="~/App_Data/names.xsl" DataFile="~/App_Data/people.xml" /> <asp:TreeView id="PeopleTreeView" runat="server" DataSourceID="PeopleDataSource"> <DataBindings> <asp:TreeNodeBinding DataMember="Name" TextField="#InnerText" /> </DataBindings> </asp:TreeView> </form> </BODY> </HTML> |
The following shows the XSL style sheet used by the preceding code example.
В | Copy Code |
---|---|
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="People"> <Names> <xsl:apply-templates select="Person"/> </Names> </xsl:template> <xsl:template match="Person"> <xsl:apply-templates select="Name"/> </xsl:template> <xsl:template match="Name"> <name><xsl:value-of select="LastName"/>, <xsl:value-of select="FirstName"/></name> </xsl:template> </xsl:stylesheet> |
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
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
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 |