Hi Friends
How i can save our data(table) in xml file and retrieve it on asp.net page?
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.
Anuja PawarPosted Jan 10, 2012, 5:36 AM
Try this
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 100px">
Name:td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server">asp:TextBox>td>
tr>
<tr>
<td style="width: 100px">
Location:td>
<td style="width: 100px">
<asp:TextBox ID="txtLocation" runat="server">asp:TextBox>td>
tr>
<tr>
<td style="width: 100px">
Email:td>
<td style="width: 100px">
<asp:TextBox ID="txtEmail" runat="server">asp:TextBox>td>
tr>
<tr>
<td>td>
<td>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />td>
tr>
table>
<br />
<asp:DataList ID="dlComments" Runat="server" Width="100%">
<ItemTemplate>
<hr size=0/>
Name: <%# DataBinder.Eval(Container.DataItem, "name") %><br />
E-mail: <a href="mailto:<%# DataBinder.Eval(Container.DataItem, "email") %>"><%#DataBinder.Eval(Container.DataItem, "email") %>a><br />
Location: <%# DataBinder.Eval(Container.DataItem, "location") %><br />
Date: <%# DataBinder.Eval(Container.DataItem, "Date") %><br />
Description: <%# DataBinder.Eval(Container.DataItem, "Description") %>
ItemTemplate>
asp:DataList>
div>
form>
body>
html>
After that add XML file to your application and give name as "Sample.xml" intially xml file like this root element is compulsary for XML files that's why I added CommentInformation in XML file that root element in XML file.
xml version="1.0" encoding="utf-8"?>
<CommentsInformation>
CommentsInformation>
After that add this namespace in codebehind
using System.Xml;
using System.Data;
After that write the following code in code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind xml data to datalist
BindDatalist();
}
}
///
/// btnSubmit event is used to insert data into XML file
///
///
///
protected void btnSubmit_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("Sample.xml"));
XmlElement parentelement = xmldoc.CreateElement("Comments");
XmlElement name = xmldoc.CreateElement("Name");
name.InnerText = txtName.Text;
XmlElement location = xmldoc.CreateElement("location");
location.InnerText = txtLocation.Text;
XmlElement email = xmldoc.CreateElement("Email");
email.InnerText = txtEmail.Text;
XmlElement date = xmldoc.CreateElement("Date");
date.InnerText = DateTime.Now.ToString();
parentelement.AppendChild(name);
parentelement.AppendChild(location);
parentelement.AppendChild(email);
parentelement.AppendChild(Description);
parentelement.AppendChild(date);
xmldoc.DocumentElement.AppendChild(parentelement);
xmldoc.Save(Server.MapPath("Sample.xml"));
BindDatalist();
}
///
/// Bind xml data to datalist
///
private void BindDatalist()
{
XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("Sample.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count != 0)
{
dlComments.DataSource = ds;
dlComments.DataBind();
}
else
{
dlComments.DataSource = null;
dlComments.DataBind();
}
}
Satyapriya NayakPosted Jan 4, 2012, 10:46 PM
For reading xml file into gridview
protected void Page_Load(object sender, EventArgs e)
{
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("test.xml"));
GridView1.DataSource = reportData;
GridView1.DataBind();
}
For writing xml file from gridview
using System;
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;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
con.Close();
ds = new DataSet();
sqlda.Fill(ds, "employee");
GridView1.DataSource = ds;
GridView1.DataMember = "employee";
GridView1.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
{
BindGrid();
ds.WriteXml("d:\\employee.xml");
Response.Write("Records saved as xml");
}
}
Thanks
VulpesPosted Jan 4, 2012, 6:24 PM
I'd check out the examples in the MSDN docs:
http://msdn.microsoft.com/en-us/library/system.data.datatable_methods.aspx