Monday, 14 October 2013

Insert delete update records in XML file -ASP.NET using c#

This is sample xml data structure we have created for this article

<?xml version="1.0" standalone="yes"?>
<EmployeeDetails>
  <Employee>
    <empid>1001</empid>
    <name>Sachin</name>
    <designation>Software Engineer</designation>
    <city>Chennai</city>
    <country>India</country>
  </Employee>
  <Employee>
    <empid>1002</empid>
    <name>Suresh</name>
    <designation>Web Developer</designation>
    <city>New Delhi</city>
    <country>India</country>
  </Employee>
  </EmployeeDetails>

Designer Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="false" ShowFooter="true"
    style="margin-left: 113px" Width="1049px"
    OnRowCommand="gvEmployee_OnRowCommand"
    onrowcancelingedit="gvEmployee_RowCancelingEdit"
    onrowdeleting="gvEmployee_RowDeleting" onrowediting="gvEmployee_RowEditing"
    onrowupdating="gvEmployee_RowUpdating"
    onpageindexchanging="gvEmployee_PageIndexChanging">
<Columns>
       
    <asp:TemplateField HeaderText="Employee ID" HeaderStyle-Width="100px">
        
    <ItemTemplate >
        <asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(
                             Container. DataItem,"empid") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>       
    </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Name" HeaderStyle-Width="100px">
    <EditItemTemplate>
        <asp:TextBox ID="txtName" runat="server" Text='<%#DataBinder.Eval(
                           Container. DataItem,"name") %>'></asp:TextBox>       
    </EditItemTemplate>
    <ItemTemplate >
        <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(
                            Container. DataItem,"name") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtAddName" runat="server"></asp:TextBox>       
    </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Designation" HeaderStyle-Width="100px">
    <EditItemTemplate>
        <asp:TextBox ID="txtDesignation" runat="server" Text='<%#DataBinder.Eval(
                            Container. DataItem,"designation") %>'></asp:TextBox>       
    </EditItemTemplate>
    <ItemTemplate >
        <asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(
                              Container. DataItem,"designation") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtAddDesignation" runat="server"></asp:TextBox>       
    </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="City" HeaderStyle-Width="100px">
    <EditItemTemplate>
        <asp:TextBox ID="txtCity" runat="server" Text='<%#DataBinder.Eval(
                            Container. DataItem,"city") %>'></asp:TextBox>       
    </EditItemTemplate>
    <ItemTemplate >
        <asp:Label ID="lblCity" runat="server" Text='<%#DataBinder.Eval(
                              Container. DataItem,"city") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtAddCity" runat="server"></asp:TextBox>       
    </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Country" HeaderStyle-Width="200px">
    <EditItemTemplate >
        <asp:TextBox ID="txtCountry" runat="server" Text='<%#DataBinder.Eval(
          Container. DataItem,"country") %>' TextMode="MultiLine"></asp:TextBox>       
    </EditItemTemplate>
    <ItemTemplate >
        <asp:Label ID="lblCountry" runat="server" Text='<%#DataBinder.Eval(
                                Container. DataItem,"country") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtAddCountry" runat="server"></asp:TextBox>       
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Action" HeaderStyle-Width="150px">
<EditItemTemplate>
    <asp:LinkButton ID="lbtnUpdate" CommandName="Update" runat="server">
                                              Update</asp:LinkButton>
    <asp:LinkButton ID="lbtnCancel"  CommandName="Cancel" runat="server">
                                              Cancel</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
    <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit">
                                               Edit</asp:LinkButton>
    <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete">
                                                Delete</asp:LinkButton>
</ItemTemplate>        
<FooterTemplate>
    <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="Add">
                                                   Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>        
</Columns>
    <HeaderStyle BackColor="#666666" Font-Names="Cambria" ForeColor="White"
        Height="30px" />
    <RowStyle Font-Names="Calibri" />
</asp:GridView>
    </div>
    </form>
</body>
</html>
 Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    protected void BindGrid()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        ds.ReadXml(Server.MapPath("EmployeeDetails.xml"));
        if (ds != null && ds.HasChanges())
        {
            gvEmployee.DataSource = ds;
            gvEmployee.DataBind();
        }
        else
        {
            gvEmployee.DataBind();
        }

    }

    protected void gvEmployee_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName.Equals("Add"))
    {
        TextBox txtAddEmpID = (TextBox)gvEmployee.FooterRow.FindControl("txtAddEmpID");
        TextBox txtAddName = (TextBox)gvEmployee.FooterRow.FindControl("txtAddName");
        TextBox txtAddDesignation = (TextBox)gvEmployee.FooterRow.FindControl("txtAddDesignation");
        TextBox txtAddCity = (TextBox)gvEmployee.FooterRow.FindControl("txtAddCity");
        TextBox txtAddCountry = (TextBox)gvEmployee.FooterRow.FindControl("txtAddCountry");

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("EmployeeDetails.xml"));
        XmlElement parentelement = xmldoc.CreateElement("Employee");

        XmlElement empid = xmldoc.CreateElement("empid");
        XmlElement name = xmldoc.CreateElement("name");
        XmlElement designation = xmldoc.CreateElement("designation");
        XmlElement city = xmldoc.CreateElement("city");
        XmlElement country = xmldoc.CreateElement("country");

        empid.InnerText = txtAddEmpID.Text;
        name.InnerText = txtAddName.Text;
        designation.InnerText = txtAddDesignation.Text;
        city.InnerText = txtAddCity.Text;
        country.InnerText = txtAddCountry.Text;
           
        parentelement.AppendChild(empid);
        parentelement.AppendChild(name);
        parentelement.AppendChild(designation);
        parentelement.AppendChild(city);
        parentelement.AppendChild(country);

        xmldoc.DocumentElement.AppendChild(parentelement);
        xmldoc.Save(Server.MapPath("EmployeeDetails.xml"));

        BindGrid();
    }
    }

    protected void gvEmployee_RowEditing(object sender, GridViewEditEventArgs e)
    {
    gvEmployee.EditIndex = e.NewEditIndex;
    BindGrid();
    }
    protected void gvEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    BindGrid();
    DataSet ds = gvEmployee.DataSource as DataSet;
    ds.Tables[0].Rows[gvEmployee.Rows[e.RowIndex].DataItemIndex].Delete();
    ds.WriteXml(Server.MapPath("EmployeeDetails.xml"));
    BindGrid();
    }
    protected void gvEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    int i = gvEmployee.Rows[e.RowIndex].DataItemIndex;
    string Name = (gvEmployee.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text;
    string Designation = (gvEmployee.Rows[e.RowIndex].FindControl("txtDesignation") as TextBox).Text;
    string City = (gvEmployee.Rows[e.RowIndex].FindControl("txtCity") as TextBox).Text;
    string Country = (gvEmployee.Rows[e.RowIndex].FindControl("txtCountry") as TextBox).Text;

    gvEmployee.EditIndex = -1;
    BindGrid();

    DataSet ds = (DataSet)gvEmployee.DataSource;
    ds.Tables[0].Rows[i]["name"] = Name;
    ds.Tables[0].Rows[i]["designation"] = Designation;
    ds.Tables[0].Rows[i]["city"] = City;
    ds.Tables[0].Rows[i]["country"] = Country;
       
    ds.WriteXml(Server.MapPath("EmployeeDetails.xml"));
    BindGrid();
    }
    protected void gvEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    gvEmployee.EditIndex = -1;
    BindGrid();
    }
    protected void gvEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    gvEmployee.PageIndex = e.NewPageIndex;
    BindGrid();
    } 

}

No comments:

Post a Comment