How to use XMLDataSource in Asp.Net



Here I have given how to use XMLDataSource example for understanding how to use it in our web application. For filtering records in webpage using XMLDataSource in dot net.

HTML CODE:
<div>
Country: <asp:DropDownList ID="ddlCountries" runat="server" OnSelectedIndexChanged="CountryChanged"
            AutoPostBack="true">
            <asp:ListItem Text="All" Value="" />
            <asp:ListItem Text="India" Value="India" />
            <asp:ListItem Text="USA" Value="USA" />
            <asp:ListItem Text="UK" Value="UK" />
        </asp:DropDownList>&nbsp;
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="Ahmedabad" Value="Ahmedabad" />
            <asp:ListItem Text="Amreli" Value="Amreli" />
            <asp:ListItem Text="Rajkot" Value="Rajkot" />
            <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"
            BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
            CellPadding="3" ForeColor="Black" GridLines="Vertical">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <Columns>
                <asp:TemplateField HeaderText="Id" HeaderStyle-Width="50">
                    <ItemTemplate>
                        <%# XPath("@Id") %>
                    </ItemTemplate>
                    <HeaderStyle Width="50px"></HeaderStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name" HeaderStyle-Width="100">
                    <ItemTemplate>
                        <%# XPath("EmployeeName") %>
                    </ItemTemplate>
                    <HeaderStyle Width="100px"></HeaderStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City" HeaderStyle-Width="100">
                    <ItemTemplate>
                        <%# XPath("@City") %>
                    </ItemTemplate>
                    <HeaderStyle Width="100px"></HeaderStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country" HeaderStyle-Width="100">
                    <ItemTemplate>
                        <%# XPath("Country") %>
                    </ItemTemplate>
                    <HeaderStyle Width="100px"></HeaderStyle>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True"></HeaderStyle>
         <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
         <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
         <SortedAscendingCellStyle BackColor="#F1F1F1" />
         <SortedAscendingHeaderStyle BackColor="#808080" />
         <SortedDescendingCellStyle BackColor="#CAC9C9" />
         <SortedDescendingHeaderStyle BackColor="#383838" />
        </asp:GridView>
    </div>

XML FILE: Employees.xml

<?xml version="1.0" standalone="yes"?>
<Employees>
  <Employee Id ="1" City ="Ahmedabad">
    <EmployeeName>Umesh Patel</EmployeeName>
    <Country>India</Country>
  </Employee>
  <Employee Id ="2" City ="Amreli">
    <EmployeeName>Chirag Patel</EmployeeName>
    <Country>India</Country>
  </Employee>
  <Employee Id ="3" City ="Rajkot">
    <EmployeeName>Kiran Kher</EmployeeName>
    <Country>India</Country>
  </Employee>
  <Employee Id ="4" City ="Ahmedabad">
    <EmployeeName>Jatin Parmar</EmployeeName>
    <Country>India</Country>
  </Employee>
  <Employee Id ="5" City = "London">
    <EmployeeName>kaushal Mishtry</EmployeeName>
    <Country>USA</Country>
  </Employee>
  <Employee Id ="6" City ="London">
    <EmployeeName>Jhanvi Patel</EmployeeName>
    <Country>UK</Country>
  </Employee>
  <Employee Id ="7" City ="London">
    <EmployeeName>King</EmployeeName>
    <Country>UK</Country>
  </Employee>
  <Employee Id ="8" City ="Seattle">
    <EmployeeName>Kajal Patel</EmployeeName>
    <Country>USA</Country>
  </Employee>
  <Employee Id ="9" City ="London">
    <EmployeeName>Anne Dodsworth</EmployeeName>
    <Country>UK</Country>
  </Employee>
</Employees>


CODE BEHIND:

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";
            }
        }
Now run your webpage and filter based on country and city name.


Related Posts

Previous
Next Post »

Thanks for comments.....