Friday 26 October 2012

create Xml file and bind to gridview in asp.net


XML file:

<?xml version="1.0" encoding="utf-8"?>
<EmpXml>
  <Emp1>
    <Name>Name1</Name>
    <Loc>Hyd</Loc>
    <Phone>12345434</Phone>
  </Emp1>
  <Emp1>
    <Name>krishna</Name>
    <Loc>Delhi</Loc>
    <Phone>90876</Phone>
  </Emp1>
  <Emp1>
    <Name>samba</Name>
    <Loc>karala</Loc>
    <Phone>898767665</Phone>
  </Emp1>
  <Emp1>
    <Name>nagapur</Name>
    <Loc>Hyd</Loc>
    <Phone>9998765</Phone>
  </Emp1>
  <Emp1>
    <Name>kumar</Name>
    <Loc>bang</Loc>
    <Phone>999999</Phone>
  </Emp1>
  <Emp1>
    <Name>naga</Name>
    <Loc>masurai</Loc>
    <Phone>87654</Phone>
  </Emp1>
  <Emp1>
    <Name>raghu</Name>
    <Loc>mumbai</Loc>
    <Phone>765433333</Phone>
  </Emp1>
</EmpXml>

sourcecode:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    </div>
    <table class="style1">
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                EmpName</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                EmpLoc</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                EmpPhone</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            </td>
        </tr>
    </table>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"      
        style="height: 133px; width: 281px">
    <Columns>
    <asp:BoundField  HeaderText="Name" DataField="Name" />
    <asp:BoundField HeaderText="Loc" DataField="Loc" />
    <asp:BoundField HeaderText="Phone" DataField="Phone" />
    <asp:CommandField ShowCancelButton="true" ShowDeleteButton="true" ShowEditButton="true" CancelText="Cancel" EditText="Edit" DeleteText="Delete" UpdateText="Update"/>
    </Columns>
     </asp:GridView>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;


public partial class LoadXmldata : System.Web.UI.Page

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind_data();
        }
    }
    private void bind_data()
    {
        DataSet ds = new DataSet();

        string filePath = Server.MapPath("EmpXml.xml");

        ds.ReadXml(filePath);

        GridView1.DataSource = ds.Tables[0].DefaultView;

        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xmldoc = new XmlDocument();

        XmlElement parentelement=xmldoc.CreateElement("Emp1");

        xmldoc.Load(Server.MapPath("EmpXml.xml"));

        XmlElement name=xmldoc.CreateElement("Name");

        name.InnerText=TextBox1.Text;

        XmlElement loc=xmldoc.CreateElement("Loc");

        loc.InnerText=TextBox2.Text;

        XmlElement phone=xmldoc.CreateElement("Phone");

        phone.InnerText=TextBox3.Text;

        parentelement.AppendChild(name);

        parentelement.AppendChild(loc);

        parentelement.AppendChild(phone);

        xmldoc.DocumentElement.AppendChild(parentelement);

        xmldoc.Save(Server.MapPath("EmpXml.xml"));

        bind_data();
    }
}

No comments:

Post a Comment