Hi ,
I want to know that how can we save and retrieve data from a XML file and explain the concept with scenario ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 3, 2012, 11:42 PM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Saving_User_Input_Data_to_an_XML._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Saving_User_Input_Data_to_an_XML
{
public partial class _Default : System.Web.UI.Page
{
static DataSet ds;
static DataTable dt;
static DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ds = new DataSet();
ds.ReadXml(MapPath("data.xml"));
dt = ds.Tables["student"];
}
}
protected void btnaddrecords_Click(object sender, EventArgs e)
{
dr = dt.NewRow();
dr["sid"] = TextBox1.Text;
dr["sname"] = TextBox2.Text;
dr["smarks"] = TextBox3.Text;
dr["saddress"] = TextBox4.Text;
dt.Rows.Add(dr);
ds.WriteXml(Server.MapPath("data.xml"));
Response.Write("Record Successfully Inserted");
clear();
}
void clear()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
protected void btn_showdata_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("data.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
Thanks
If this post helps you mark it as answer