Hello, I will explain CRUD (Create, Read Update Delete) operation in XML with the help of ASP.NET DetailsView control. In this post I am explaining to read and write XML file in asp.net using c#.
- Open Visual Studio
- Create a new Empty Web Site
- Add new item>WebForm>name it(xmlinsertion.aspx)
I have created one MasterPage, aspx page with four textbox to enter details and DetailsView control to display data and also delete and update data.
HTML source of page xmlinsertion.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="xmlinsertion.aspx.cs" Inherits="xmlinsertion" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
.style1
{
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div class="datagrid">
<table>
<thead>
<tr>
<th colspan="2">
XML Demo
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="style1">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Name:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr class="alt">
<td class="style1">
Address:
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
Email:
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr class="alt">
<td class="style1">
Comment:
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr class="alt">
<td class="style1" colspan="2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Insert" />
<asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="Cancel" />
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px"
OnItemCommand="DetailsView1_ItemCommand" OnItemDeleting="DetailsView1_ItemDeleting"
Width="125px" DataKeyNames="Id" OnModeChanging="DetailsView1_ModeChanging" OnPageIndexChanging="DetailsView1_PageIndexChanging1"
AllowPaging="true" DefaultMode="ReadOnly">
<Fields>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("comment") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="Button4" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<br />
<br />
</td>
</tr>
</tbody>
</table>
</div>
</asp:Content>
HTML source of page MasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<h1 class="Title">
XML DEMO</h1>
<span class="TagLine">This Is XML DEMO</span>
<ul class="Menu">
<li><a href="xmlinsertion.aspx">FillUp</a></li>
</ul>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
C# code of xmlinsertion.aspx.cs
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.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.IO;
public partial class xmlinsertion : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationTable"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddetailview();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("UserInfo");
if (Button1.Text == "Update")
{
int datakeys = Convert.ToInt32(DetailsView1.DataKey["Id"].ToString());
int lblid = Convert.ToInt32(ViewState["LblId"].ToString());
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("samplexml.xml"));
XmlNodeList nodes = xmldoc.SelectNodes("NewDataSet/UserInfo[Id=" + lblid + "]");
foreach (XmlNode node in nodes)
{
XmlNodeList childnodes = node.ChildNodes;
foreach (XmlNode childnode in childnodes)
{
switch (childnode.Name)
{
case "name":
childnode.InnerText = TextBox1.Text;
break;
case "address":
childnode.InnerText = TextBox2.Text;
break;
case "email":
childnode.InnerText = TextBox3.Text;
break;
case "comment":
childnode.InnerText = TextBox4.Text;
break;
}
}
childnodes = null;
break;
}
xmldoc.Save(Server.MapPath("samplexml.xml"));
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
DetailsView1.DefaultMode = DetailsViewMode.ReadOnly;
binddetailview();
// here Write Remaining Code from XMLGENERATE FILE
}
else
{
if (File.Exists(Server.MapPath("samplexml.xml")))
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("samplexml.xml"));
XmlTextReader xreader = new XmlTextReader(Server.MapPath("samplexml.xml"));
ds.ReadXml(xreader);
DataTable dt1 = new DataTable();
DataTableReader dtreader = new DataTableReader(ds.Tables["UserInfo"]);
dt1.Load(dtreader);
DataRow dtr = dt1.NewRow();
dtr["name"] = TextBox1.Text;
dtr["address"] = TextBox2.Text;
dtr["email"] = TextBox3.Text;
dtr["comment"] = TextBox4.Text;
dt1.Rows.Add(dtr);
xreader.Close();
dt1.AcceptChanges();
dt1.WriteXml(Server.MapPath("samplexml.xml"), XmlWriteMode.WriteSchema);
}
else
{
DataColumn dtc = new DataColumn("Id", typeof(System.Int32));
DataColumn dtc1 = new DataColumn("name", typeof(System.String));
DataColumn dtc2 = new DataColumn("address", typeof(System.String));
DataColumn dtc3 = new DataColumn("email", typeof(System.String));
DataColumn dtc4 = new DataColumn("comment", typeof(System.String));
dtc.AutoIncrement = true;
dtc.AutoIncrementSeed = 1;
dtc.AutoIncrementStep = 1;
dtc.ReadOnly = true;
dt.Columns.Add(dtc);
dt.Columns.Add(dtc1);
dt.Columns.Add(dtc2);
dt.Columns.Add(dtc3);
dt.Columns.Add(dtc4);
DataRow dtr = dt.NewRow();
dtr["name"] = TextBox1.Text;
dtr["address"] = TextBox2.Text;
dtr["email"] = TextBox3.Text;
dtr["comment"] = TextBox4.Text;
dt.Rows.Add(dtr);
ds.Tables.Add(dt);
dt.WriteXml(Server.MapPath("samplexml.xml"), XmlWriteMode.WriteSchema);
}
}
binddetailview();
}
protected void binddetailview()
{
if (File.Exists(Server.MapPath("samplexml.xml")))
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("samplexml.xml"));
XmlTextReader xreader = new XmlTextReader(Server.MapPath("samplexml.xml"));
DataTable ds = new DataTable();
ds.ReadXml(xreader);
xreader.Close();
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
if (DetailsView1.DefaultMode != DetailsViewMode.Edit)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
Button1.Text = "Insert";
}
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
binddetailview();
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
Label lblId = (Label)DetailsView1.FindControl("Label2");
Label lblname = (Label)DetailsView1.FindControl("Label3");
Label lbladdress = (Label)DetailsView1.FindControl("Label4");
Label lblemail = (Label)DetailsView1.FindControl("Label5");
Label lblcomment = (Label)DetailsView1.FindControl("Label6");
TextBox1.Text = lblname.Text;
TextBox2.Text = lbladdress.Text;
TextBox3.Text = lblemail.Text;
TextBox4.Text = lblcomment.Text;
Button1.Text = "Update";
ViewState["LblId"] = lblId.Text.ToString();
}
}
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("samplexml.xml"));
ds.Tables["UserInfo"].Rows.RemoveAt(e.RowIndex);
DataTable dt = (DataTable)ds.Tables["UserInfo"];
dt.WriteXml(Server.MapPath("samplexml.xml"), XmlWriteMode.WriteSchema);
binddetailview();
}
protected void DetailsView1_PageIndexChanging1(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
binddetailview();
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.NewMode == DetailsViewMode.Edit)
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
DetailsView1.DefaultMode = (DetailsViewMode.Edit);
binddetailview();
}
}
protected void Button5_Click(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
DetailsView1.DefaultMode = DetailsViewMode.ReadOnly;
binddetailview();
}
}
Run the application and see the output as below.
Thanks For Comment... Suhasini :)
ReplyDelete