In this article I will explain how to use ASP.Net XmlDataSource control and populate results from XML file and bind it to ASP.Net GridView Control.
This article also explains how to filter results from XmlDataSource using XPath Query at runtime in two ways
1. Filtering XmlDataSource based on data in XML Tag.
2. Filtering XmlDataSource based on data in XML Attribute.
You might also like to read:
XML
Below is the XML file that we will be populated in GridView using ASP.Net XmlDataSource control, this XML contains data for Employees in Tags and Attributes. For example, EmployeeName and Country is populated in Tags while the Id and City are populated in Attributes.
<?xml version="1.0" standalone="yes"?>
<Employees>
<Employee Id ="1" City ="Seattle">
<EmployeeName>Nancy Davolio</EmployeeName>
<Country>USA</Country>
</Employee>
<Employee Id ="2" City ="Tacoma">
<EmployeeName>Andrew Fuller</EmployeeName>
<Country>USA</Country>
</Employee>
<Employee Id ="3" City ="Kirkland">
<EmployeeName>Janet Leverling</EmployeeName>
<Country>USA</Country>
</Employee>
<Employee Id ="4" City ="Redmond">
<EmployeeName>Margaret Peacock</EmployeeName>
<Country>USA</Country>
</Employee>
<Employee Id ="5" City = "London">
<EmployeeName>Steven Buchanan</EmployeeName>
<Country>UK</Country>
</Employee>
<Employee Id ="6" City ="London">
<EmployeeName>Michael Suyama</EmployeeName>
<Country>UK</Country>
</Employee>
<Employee Id ="7" City ="London">
<EmployeeName>Robert King</EmployeeName>
<Country>UK</Country>
</Employee>
<Employee Id ="8" City ="Seattle">
<EmployeeName>Laura Callahan</EmployeeName>
<Country>USA</Country>
</Employee>
<Employee Id ="9" City ="London">
<EmployeeName>Anne Dodsworth</EmployeeName>
<Country>UK</Country>
</Employee>
</Employees>
HTML Markup
The HTML Markup consists two DropDownLists, one to display list of Countries and other for Cities. These DropDownLists will be used to explain how to filter XmlDataSource using data from Tags and using data from Attributes.
There is also an ASP.Net GridView control to display the XML contents. For the GridView we have set the following additional parameters as we are making use of XmlDataSource.
DataSourceID: - Id of the XmlDataSource control.
XPath: - Path to the Tag that needs to be populated inside the GridView.
Note: While binding Attribute @ prefix is used in the XPath before the name of the Attribute which is not used while binding Tag, this is necessary to differentiate between XML Tag and Attribute.
Finally there’s an ASP.Net XmlDataSource control for which we have set the Path of the XML file discussed earlier.
Country:
<asp:DropDownList ID="ddlCountries" runat="server" OnSelectedIndexChanged="CountryChanged"
AutoPostBack="true">
<asp:ListItem Text="All" Value="" />
<asp:ListItem Text="USA" Value="USA" />
<asp:ListItem Text="UK" Value="UK" />
</asp:DropDownList>
City:
<asp:DropDownList ID="ddlCities" runat="server" OnSelectedIndexChanged="CityChanged"
AutoPostBack="true">
<asp:ListItem Text="All" Value="" />
<asp:ListItem Text="Seattle" Value="Seattle" />
<asp:ListItem Text="Tacoma" Value="Tacoma" />
<asp:ListItem Text="Kirkland" Value="Kirkland" />
<asp:ListItem Text="Redmond" Value="Redmond" />
<asp:ListItem Text="London" Value="London" />
</asp:DropDownList>
<hr />
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Employees.xml">
</asp:XmlDataSource>
<asp:GridView ID="GridView1" runat="server" XPath="/Employees/Employee" DataSourceID="XmlDataSource1"
AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
<Columns>
<asp:TemplateField HeaderText="Id" HeaderStyle-Width="50">
<ItemTemplate>
<%# XPath("@Id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-Width="100">
<ItemTemplate>
<%# XPath("EmployeeName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" HeaderStyle-Width="100">
<ItemTemplate>
<%# XPath("@City") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" HeaderStyle-Width="100">
<ItemTemplate>
<%# XPath("Country") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Filtering XmlDataSource based on XML Tags
When the Country DropDownList is changed the following event handler is raised, it first checks whether the selected item is not the default item, if No then using XPATH it matches the Country selected in the DropDownList with the data in the Country Tag of the XML and only records of the employees whose country matches with the value selected in the Country DropDownList.
If the default item is selected, then all items in the XML i.e. all employee records are displayed.
C#
protected void CountryChanged(object sender, EventArgs e)
{
ddlCities.SelectedIndex = -1;
string country = ddlCountries.SelectedItem.Value;
if (country != string.Empty)
{
XmlDataSource1.XPath = "/Employees/Employee[ Country='" + country + "']";
}
else
{
XmlDataSource1.XPath = "/Employees/Employee";
}
}
VB.Net
Protected Sub CountryChanged(sender As Object, e As EventArgs)
ddlCities.SelectedIndex = -1
Dim country As String = ddlCountries.SelectedItem.Value
If country <> String.Empty Then
XmlDataSource1.XPath = "/Employees/Employee[ Country='" & country & "']"
Else
XmlDataSource1.XPath = "/Employees/Employee"
End If
End Sub
Filtering XmlDataSource based on XML Attributes
When the City DropDownList is changed the following event handler is raised, it first checks whether the selected item is not the default item, if No then using XPATH it matches the City selected in the DropDownList with the data in the City Attribute of the XML and only records of the employees whose city matches with the value selected in the City DropDownList.
Note: While matching Attribute in XPATH Query, @ prefix is used before the name of the Attribute which was not used while matching Tag.
If the default item is selected, then all items in the XML i.e. all employee records are displayed.
C#
protected void CityChanged(object sender, EventArgs e)
{
ddlCountries.SelectedIndex = -1;
string city = ddlCities.SelectedItem.Value;
if (city != string.Empty)
{
XmlDataSource1.XPath = "/Employees/Employee[ @City='" + city + "']";
}
else
{
XmlDataSource1.XPath = "/Employees/Employee";
}
}
VB.Net
Protected Sub CityChanged(sender As Object, e As EventArgs)
ddlCountries.SelectedIndex = -1
Dim city As String = ddlCities.SelectedItem.Value
If city <> String.Empty Then
XmlDataSource1.XPath = "/Employees/Employee[ @City='" & city & "']"
Else
XmlDataSource1.XPath = "/Employees/Employee"
End If
End Sub
Demo
Downloads