Partager via


XmlDataSource Web Server Control Overview

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

For information about how to configure the XmlDataSource control by using code instead of markup, see the documentation for the XmlDataSource class and for its methods, properties, and events.

This topic contains:

  • Background

  • Code Examples

  • Class Reference

Background

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 Stylesheet 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.

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

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" 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>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" 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.

<?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 Stylesheet 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.

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" 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>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" 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.

<xsl:stylesheet version="1.0" xmlns:xsl="https://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 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.

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.

Back to top

Code Examples

Walkthrough: Creating a Web Page to Display XML Data

Back to top

Class Reference

The following table lists the key classes that relate to the XmlDataSource control.

Member

Description

XmlDataSource

The main class for the control.

Back to top

See Also

Reference

XML Web Server Control Overview