A better way to deal with Xml contents for modifying,inserting and deleting is to convert your xml file into DataTable with the help of DataSet.ReadXml and then call "Add","Delete",and do updating and then use WriteXml into the file。
If you are interested in this,please leave your email and I'll send you a copy of that。
I've sent you the email with the package of full sample codes,please check that——
Here's the sample codes:
【xml】
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.
</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>
A deep sea diver finds true love twenty
thousand leagues beneath the sea.
</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>
An anthology of horror stories about roaches,
centipedes, scorpions and other insects.
</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>
After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.
</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>
Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.
</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.
</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>
Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.
</description>
</book>
</DocumentElement>
// Find each row of the GridView
$("tr").each(function () {
// Find each table cell of the row and find the second linkbutton,
// Add the confirm dialog before deleting.
$(this).find("td:eq(0)>a:eq(1)").click(function () {
/****************************** Module Header ******************************\
* Module Name: Default.aspx.cs
* Project: CSASPNETCRUDXmlInGridView
* Copyright (c) Microsoft Corporation
*
* The project shows up how to insert/delete/update a record into the xml file
* by the GridView.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
\***************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace XmlEditInsertDeleteInGridView
{
public partial class Default : System.Web.UI.Page
{
/// <summary>
/// For the first time when the page loads, load data into DataTable
/// with DataSet, and save the DataTable into ViewState for further
/// usage.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Request.MapPath("try.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ViewState["dt"] = ds.Tables[0];
}
}
/// <summary>
/// Handle the Edit event of GridView for assigning the specific row to be
/// in the edit mode.
/// </summary>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = (DataTable)ViewState["dt"];
GridView1.DataBind();
}
/// <summary>
/// Update the specific row in the DataTable with the data from GridView,
/// re-write the data into the xml file and re-databind again.
/// </summary>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
for (int i = 1; i < GridView1.Rows[e.RowIndex].Cells.Count; i++)
{
dt.Rows[e.RowIndex][i-1] = (GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text;
}
dt.AcceptChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
dt.WriteXml(Request.MapPath("try.xml"));
}
/// <summary>
/// Cancel edit and set the mode of the GridView to normal viewing mode.
/// </summary>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["dt"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Insert the data into the DataTable, re-write into the xml file and
/// re-databind to the GridView.
/// </summary>
protected void btnInsert_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.Add(tbAuthor.Text, tbTitle.Text, tbGenre.Text, tbPrice.Text, tbPublishDate.Text, tbDescription.Text, tbId.Text);
dt.AcceptChanges();
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Delete the row from DataTable and write data into xml file,
/// re-databind to the GridView.
/// </summary>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.RemoveAt(e.RowIndex);
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
Marked as answer by shan000 on Feb 06, 2012 11:29 AM
shan000
Member
491 Points
446 Posts
how to read/write in xml
Feb 03, 2012 06:47 PM|LINK
Hi,
im begineer in asp.net, im devloping a web application regarding classifeids.
it contains a categories or subcategories.
I want to save those is xml files. Please give me code or reference as i dnno how to create / read/write in XML file using C#
Thanks
Shan
Please Visit : www.classifiedspak.com
Shellymn
Contributor
2602 Points
485 Posts
Re: how to read/write in xml
Feb 03, 2012 07:10 PM|LINK
Please go through this article from microsoft ( explains really well)
http://msdn.microsoft.com/en-us/magazine/cc164142.aspx
tom22
Member
246 Points
70 Posts
Re: how to read/write in xml
Feb 03, 2012 10:25 PM|LINK
have a look at this
http://www.codeproject.com/Articles/7718/Using-XML-in-C-in-the-simplest-way
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to read/write in xml
Feb 05, 2012 12:52 AM|LINK
Hello shan00:)
A better way to deal with Xml contents for modifying,inserting and deleting is to convert your xml file into DataTable with the help of DataSet.ReadXml and then call "Add","Delete",and do updating and then use WriteXml into the file。
If you are interested in this,please leave your email and I'll send you a copy of that。
Reguards!
shan000
Member
491 Points
446 Posts
Re: how to read/write in xml
Feb 05, 2012 07:36 AM|LINK
shanali786@gmail.com is my mail.
Thanks ,
Please Visit : www.classifiedspak.com
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to read/write in xml
Feb 05, 2012 07:50 AM|LINK
OK,I'll send you at my free time when in work within 5 days。
Reguards!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to read/write in xml
Feb 06, 2012 12:36 AM|LINK
Hello again:)
I've sent you the email with the package of full sample codes,please check that——
Here's the sample codes:
【xml】
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.
</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>
A deep sea diver finds true love twenty
thousand leagues beneath the sea.
</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>
An anthology of horror stories about roaches,
centipedes, scorpions and other insects.
</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>
After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.
</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>
Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.
</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>
The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.
</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>
Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.
</description>
</book>
</DocumentElement>
【aspx】
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XmlEditInsertDeleteInGridView.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>Xml-based CRUD Code Sample</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
<script type="text/javascript">
$(function () {
// Find each row of the GridView
$("tr").each(function () {
// Find each table cell of the row and find the second linkbutton,
// Add the confirm dialog before deleting.
$(this).find("td:eq(0)>a:eq(1)").click(function () {
return confirm("Are you sure to delete?");
});
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Xml-based CRUD Code Sample——</h2>
<asp:GridView ID="GridView1" runat="server" Width="70%" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
</div>
<p>
Id:<asp:TextBox ID="tbId" runat="server"></asp:TextBox>
</p>
<p>
author:<asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>
</p>
<p>
title:<asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>
</p>
<p>
genre:<asp:TextBox ID="tbGenre" runat="server"></asp:TextBox>
</p>
<p>
price:<asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
</p>
<p>
publishdate:<asp:TextBox ID="tbPublishDate" runat="server"></asp:TextBox>
</p>
<p>
description:<asp:TextBox ID="tbDescription" runat="server"></asp:TextBox>
</p>
<asp:Button ID="btnInsert" runat="server" Height="26px" OnClick="btnInsert_Click"
Text="Insert" Width="141px" />
</form>
</body>
</html>
【cs】
/****************************** Module Header ******************************\
* Module Name: Default.aspx.cs
* Project: CSASPNETCRUDXmlInGridView
* Copyright (c) Microsoft Corporation
*
* The project shows up how to insert/delete/update a record into the xml file
* by the GridView.
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
\***************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace XmlEditInsertDeleteInGridView
{
public partial class Default : System.Web.UI.Page
{
/// <summary>
/// For the first time when the page loads, load data into DataTable
/// with DataSet, and save the DataTable into ViewState for further
/// usage.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Request.MapPath("try.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ViewState["dt"] = ds.Tables[0];
}
}
/// <summary>
/// Handle the Edit event of GridView for assigning the specific row to be
/// in the edit mode.
/// </summary>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = (DataTable)ViewState["dt"];
GridView1.DataBind();
}
/// <summary>
/// Update the specific row in the DataTable with the data from GridView,
/// re-write the data into the xml file and re-databind again.
/// </summary>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
for (int i = 1; i < GridView1.Rows[e.RowIndex].Cells.Count; i++)
{
dt.Rows[e.RowIndex][i-1] = (GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text;
}
dt.AcceptChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
dt.WriteXml(Request.MapPath("try.xml"));
}
/// <summary>
/// Cancel edit and set the mode of the GridView to normal viewing mode.
/// </summary>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataTable dt = (DataTable)ViewState["dt"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Insert the data into the DataTable, re-write into the xml file and
/// re-databind to the GridView.
/// </summary>
protected void btnInsert_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.Add(tbAuthor.Text, tbTitle.Text, tbGenre.Text, tbPrice.Text, tbPublishDate.Text, tbDescription.Text, tbId.Text);
dt.AcceptChanges();
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Delete the row from DataTable and write data into xml file,
/// re-databind to the GridView.
/// </summary>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.RemoveAt(e.RowIndex);
dt.WriteXml(Request.MapPath("try.xml"));
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to read/write in xml
Feb 06, 2012 12:38 AM|LINK
Oh man!Plz don't use gmail!It has refused my sending!!!
Plz change to hotmail instead。
And I'll resend to you……
shan000
Member
491 Points
446 Posts
Re: how to read/write in xml
Feb 06, 2012 05:34 AM|LINK
Gmail is best and more user friendly as compared to hotmail so i shifted to gmail :P
anywaz its street-burner@hotmail.com
Please Visit : www.classifiedspak.com
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to read/write in xml
Feb 06, 2012 11:03 AM|LINK
Sent it to you。
Reguards!