Tuesday 17 June 2014

Using XML Data Source Control in Asp.net and binding to grid and controlling events to it.

This article also explains how to make use of XML datasource control in your website for a grid and filter the data according to your selection in dropdown list.
1. Filtering XmlDataSource based on data in XML Tag.
2. Filtering XmlDataSource based on data in XML Attribute.
Below is My XML Code
<employees>
  <employee city="Kansas" id="1">
    <employeename>Roddam Ramesh</employeename>
    <country>USA</country>
  </employee>
  <employee city="Hyderabad" id="2">
    <employeename>Vishwas</employeename>
    <country>India</country>
  </employee>
  <employee city="MaryVille" id="3">
    <employeename>Dheeraj</employeename>
    <country>USA</country>
  </employee>
  <employee city="MaryVille" id="4">
    <employeename>Anil Kumar</employeename>
    <country>USA</country>
  </employee>
  <employee city="Hyderabad" id="5">
    <employeename>Satish</employeename>
    <country>India</country>
  </employee>
  <employee city="Miami" id="6">
    <employeename>Balaji</employeename>
    <country>USA</country>
  </employee>
  <employee city="Sydney" id="7">
    <employeename>Hari Krishna</employeename>
    <country>AUS</country>
  </employee>
  <employee city="Delhi" id="8">
    <employeename>samanth</employeename>
    <country>India</country>
  </employee>
  <employee city="Bombay" id="9">
    <employeename>Karan</employeename>
    <country>India</country>
  </employee>
</employees>



Set the DataSourceId and Path: 
DataSourceID: - Id of the XmlDataSource control.
XPath: - Path to the Tag that needs to be populated inside the GridView

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

For City and Country drop down Events use the following code which will filter your data acoording to the selection by the user on the drop down.

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";
    }
}

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";
    }
}

Here are the different outputs: