asp.net.ph

Skip Navigation Links

Filtering Data Using the XmlDataSource Control

Controls You Can Use on Web Forms   ASP.NET Data Source Controls   XmlDataSource Control


You can filter the XML data exposed by an XmlDataSource control by setting the control’s XPath property to an XPath filtering expression. If you have specified an Extensible Stylesheet Language (XSL) style sheet to transform the data exposed by your XmlDataSource control, the XPath filtering expression is applied after the transformation takes place.

The following code example shows an XmlDataSource control bound to a TreeView control. The XML data is filtered using an XPath query.

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

<script runat="server">

void SelectRegion(object sender, EventArgs e)
{
   if (RegionDropDownList.SelectedValue == "(Show All)")
      PeopleDataSource.XPath = "/People/Person";
   else
   {
      string selectedValue = "";

      switch (RegionDropDownList.SelectedValue)
      {
         case "CA":
            selectedValue = "CA";
            break;
         case "HI":
            selectedValue = "HI";
            break;
         case "WA":
            selectedValue = "WA";
            break;
         default:
            // Invalid value.
            break;
      }

      PeopleDataSource.XPath = "/People/Person[Address/Region='" + selectedValue + "']";
   }

   PeopleTreeView.DataBind();
}

</script>

<html>
   <body>
      <form runat="server">
         <table border=0 cellpadding=3>
            <tr>
               <td valign=top>
                  <b>select region:</b>
                  <asp:dropdownlist runat="server" id="regiondropdownlist"
                        autopostback
                        onselectedindexchanged="selectregion">
                     <asp:listitem selected="true">(show all)</asp:listitem>
                     <asp:listitem>ca</asp:listitem>
                     <asp:listitem>hi</asp:listitem>
                     <asp:listitem>wa</asp:listitem>
                  </asp:dropdownlist>
               </td>
               <td valign=top>
                  <asp:xmldatasource
                     id="peopledatasource"
                     runat="server"
                     xpath="/people/person"
                     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>
               </td>
            </tr>
         </table>
      </form>
   </body>
</html>
  C# VB



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note