Ads 468x60px

For New Update Use this Link Offer for you


Friday, 14 June 2013

read data from xml file in

read data from xml file in
Categories:-aspdotnet interview question and answer , create wcf restful web service in

Introduction :-  

Here i am explain how i can retrieve  data from XML  file in show the data of XML file on the web form.

Description :-

XML stands for Extensible Markup Language.XML is a markup language much like HTML. Using xml we can store the data and we can easily retrieve and display the data without using database in our application.Using XML no need to connect any database. If  we  want to display data dynamically then we use the XML file.On the XML file we can Perform the read write and update  operation .
Now  i will explain how i can read data from xml in

 Design your aspx page as show below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlpageread.aspx.cs" Inherits="xml_file_xmlpageread" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <style type="text/css">
        .style1 {
            text-align: center;
    <form id="form1" runat="server">
    <div class="style1">
        Enter ID
        <asp:TextBox ID="txt_id" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        <br />
        <br />
        <br />

In the above aspx page design i have taken a one textobx and on button .Textbox use for insert the Employee id  which present in xml file and press submit button get the detail related to this employee id.

code behind file as show below

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class xml_file_xmlpageread : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)

            string empid1 = txt_id.Text.ToString();
            if (empid1 != null)
                //Response.Write("<br>XML file searching using<br><br>");
                XmlDocument doc = new XmlDocument();

                string xmlFile = System.Web.HttpContext.Current.Server.MapPath("~/XMLFile.xml");
                int empid = int.Parse(empid1);// convert the empid1 to int

                XmlNodeList xmlnode = doc.GetElementsByTagName("blockid"); //this is tag of xml file
                for (int i = 0; i < xmlnode.Count; i++)

                    XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;
                    if (int.Parse(xmlattrc[0].Value) == empid)
                        Response.Write("<br>Employee-ID: " + xmlattrc[0].Value);
                        Response.Write("<br>First Name: " + xmlnode[i].ChildNodes[0].InnerText);     
        catch (Exception exp)

Xml Form at run time as show below

Enter the Employee Id Into the textbox and press submit button the display the output as shown below
Download sample code attached

No comments:

Post a Comment



New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result