Go to DotNetFunda.com
 Online : 609 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Reading Selected Element from XML using ASP.NET, C#

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

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

 Posted on: 11/25/2008 8:23:38 AM by Majith | Views: 6960 | Category: ASP.NET | Level: Beginner | Print Article
I am going to explain about the reading data from xml and filtering particular element and binding into Grid view using ASP.NET ,C#.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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();
            doc.Load(Server.MapPath("place.xml"));
            XmlNodeList bookList = doc.GetElementsByTagName("Place");
            foreach (XmlNode node in bookList)
            {
                XmlElement bookElement = (XmlElement)node;
                string ctry= bookElement.GetElementsByTagName("Country")[0].InnerText;
                ddl.Items.Add(ctry);
           }
        }
   }

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");
        dt.Columns.Add(dc);
        dt.Columns.Add(dc1);
        string ct = ddl.SelectedItem.ToString();            
        string dataPath = Server.MapPath("place.xml");
        DataSet dSet = new DataSet();
        dSet.ReadXml(dataPath);
        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"];
            dt.Rows.Add(myRow);
        }
        grxml.DataSource = dt;
        grxml.DataBind();
    }

Step 6. The Xml file as follow as

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

Step 7. The Inline code as shows below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Readxml.aspx.cs" Inherits="Readxml" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head runat="server">
   <title>Reading XML</title>
   </head>
<body>
    <form id="form1" runat="server">
    <div>
    <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">
    </asp:GridView> 
    </form>
</body>
</html>

Hope this article will help for asp.net, xml developers.


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About Majith Basha

Experience:0 year(s)
Home page:
Member since:Friday, July 18, 2008
Level:Starter
Status: [Member]
Biography:
 Latest post(s) from Majith

   ◘ Checking Existing Records Using Array of Data Rows posted on 4/13/2009 5:41:54 AM
   ◘ Finding Curosr Position Using JavaScript and Drag and Drop from WebControls posted on 2/24/2009 10:24:45 PM
   ◘ Reading Selected Element from XML using ASP.NET, C# posted on 11/25/2008 8:23:38 AM
   ◘ Java Script GridView ClientSide Validation posted on 9/30/2008 12:22:12 AM
   ◘ Diff Between DataGrid and GridView posted on 9/17/2008 11:20:35 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)