Reading Selected Element from XML using ASP.NET, C#

Posted by in ASP.NET category on for Beginner level | Views : 51336 red flag

I am going to explain about the reading data from xml and filtering particular element and binding into Grid view using ASP.NET ,C#.
Reading Selected Elements from XML

I am going to explain about reading  the data from xml and filtering particular element and binding into Grid view using ASP.NET ,C#.

We can achieve this using Dynamic Data table. We have to create a data table and data row. Lets see how to achieve this in few steps.

Step 1. Add and Dropdownlist control ,Gridview Control and Button.

Step 2. Here  I have Created an XML file named places.xml .

Step 3. It includes the following Elements.

             (i). Id

             (ii). Country

             (iii). City

Step 4. The Dropdown Control  contains the Country to filter the city.

protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)
            XmlDocument doc = new XmlDocument();
            XmlNodeList bookList = doc.GetElementsByTagName("Place");
            foreach (XmlNode node in bookList)
                XmlElement bookElement = (XmlElement)node;
                string ctry= bookElement.GetElementsByTagName("Country")[0].InnerText;

The above code is used to bind the Country first in the Dropdown list.

Step 5. Create and Data table and add the Coulmns  (1) Id  (2) City and in the Button Click add the following Code.

protected void Button1_Click(object sender, EventArgs e)
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("Id");
        DataColumn dc1 = new DataColumn("Name");
        string ct = ddl.SelectedItem.ToString();            
        string dataPath = Server.MapPath("place.xml");
        DataSet dSet = new DataSet();
        DataRow[] rows = dSet.Tables[0].Select(" Name = '" + ct + "'");
        foreach (DataRow dr in rows)
            DataRow myRow = dt.NewRow();
            myRow["Id"] = dr["Id"];
            myRow["Name"] = dr["Name"];
        grxml.DataSource = dt;

Step 6. The Xml file as follow as

<?xml version="1.0" encoding="utf-8" ?>
    <Id> 1 </Id>
    <Country>INDIA </Country>
    <Id> 2 </Id>
    <City> Atlanta </City>
    <Country>USA </Country>

Step 7. The Inline code as shows below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Readxml.aspx.cs" Inherits="Readxml" %>
<html xmlns="" >
   <head runat="server">
   <title>Reading XML</title>
    <form id="form1" runat="server">
    <asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>&nbsp;<br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    <asp:GridView ID="grxml" runat="server">

Hope this article will help for, xml developers.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Majith Basha
Member Level:
Member Status: Member
Member Since: 7/18/2008 11:49:59 PM

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)