Hi I am using XML document to Retrieve, Save and delete my data into a DataGridView table. But I don't know how can I delete the data. here is my code for adding and saving the data, user can select a row which contain this data from table in my application
GUI. So for deleting, I want user to select the row and click the Delete button to delete the data. but I don't know what is the command for deleting. :
Dim ql As XElement = (From ls In xmlTestCode.<COULTERDXI>.<TESTCODE> _ Where (CType(ls.Element("MACHINE_PANEL"), String) = txtMachineCode.Text.Trim) _ Select ls.Element("MACHINE_RESULT")).FirstOrDefault ql.SetValue(txtMachineResult.Text.Trim) xmlTestCode.Save("XML\TESTCODES.xml")
If possible,I'd like to show you how to do CRUD things with the xml file contents——Just import your xml file into DataTable or DataSet and then handle GridView's events.
For free sample codes plz leave your email:)
【Solutions】
[xml file]
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<book id="bk105">
<author>Corets, Eva & BB</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.vb
'* Project: ASPNETCRUDXmlInGridView
'* 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.
'\**************************************************************************
''' <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 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim ds As New DataSet()
ds.ReadXml(Request.MapPath("try.xml"))
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
ViewState("dt") = ds.Tables(0)
End If
End Sub
''' <summary>
''' Handle the Edit event of GridView for assigning the specific row to be
''' in the edit mode.
''' </summary>
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = DirectCast(ViewState("dt"), DataTable)
GridView1.DataBind()
End Sub
''' <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 Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
For i As Integer = 1 To GridView1.Rows(e.RowIndex).Cells.Count - 1
dt.Rows(e.RowIndex)(i - 1) = TryCast(GridView1.Rows(e.RowIndex).Cells(i).Controls(0), TextBox).Text
Next
dt.AcceptChanges()
GridView1.EditIndex = -1
GridView1.DataSource = dt
GridView1.DataBind()
dt.WriteXml(Request.MapPath("try.xml"))
End Sub
''' <summary>
''' Cancel edit and set the mode of the GridView to normal viewing mode.
''' </summary>
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
''' <summary>
''' Insert the data into the DataTable, re-write into the xml file and
''' re-databind to the GridView.
''' </summary>
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
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()
End Sub
''' <summary>
''' Delete the row from DataTable and write data into xml file,
''' re-databind to the GridView.
''' </summary>
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
dt.Rows.RemoveAt(e.RowIndex)
dt.WriteXml(Request.MapPath("try.xml"))
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
farshadak200...
0 Points
1 Post
Visual basc and XML data
Apr 15, 2012 03:57 PM|LINK
Hi
I am using XML document to Retrieve, Save and delete my data into a DataGridView table. But I don't know how can I delete the data. here is my code for adding and saving the data, user can select a row which contain this data from table in my application GUI. So for deleting, I want user to select the row and click the Delete button to delete the data. but I don't know what is the command for deleting. :
Dim Testcode = <TESTCODE>HIS_RESULT_CODE
<MACHINE_PANEL><%= txtMachineCode.Text.Trim %></MACHINE_PANEL>
<MACHINE_RESULT><%= txtMachineResult.Text.Trim %></MACHINE_RESULT>
<HIS_TEST_CODE><%= txtHISTestCode.Text.Trim %></HIS_TEST_CODE>
<HIS_RESULT_CODE><%= txtHISResultCode.Text.Trim %></HIS_RESULT_CODE>
<HIS_TEST_DESC><%= txtHISTestDesc.Text.Trim %></HIS_TEST_DESC>
<ACTIVE><%= cmbActive.Text.Trim %></ACTIVE>
<TimeStamp><%= Now.ToString("dd MMMM yyyy") %></TimeStamp>
</TESTCODE>
xmlTestCode.<COULTERDXI>(0).Add(Testcode)
Dim ql As XElement = (From ls In xmlTestCode.<COULTERDXI>.<TESTCODE> _
Where (CType(ls.Element("MACHINE_PANEL"), String) = txtMachineCode.Text.Trim) _
Select ls.Element("MACHINE_RESULT")).FirstOrDefault
ql.SetValue(txtMachineResult.Text.Trim)
xmlTestCode.Save("XML\TESTCODES.xml")
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Visual basc and XML data
Apr 17, 2012 01:58 AM|LINK
Hello farshadak2004:)
If possible,I'd like to show you how to do CRUD things with the xml file contents——Just import your xml file into DataTable or DataSet and then handle GridView's events.
For free sample codes plz leave your email:)
【Solutions】
[xml file]
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<book id="bk105">
<author>Corets, Eva & BB</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 codes]
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="VBASPNetCRUDXmlInGridView._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 id="Head1" 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>
[VB.net code-behind]
'***************************** Module Header ******************************\
'* Module Name: Default.aspx.vb
'* Project: ASPNETCRUDXmlInGridView
'* 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.
'\**************************************************************************
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Public Class _Default
Inherits 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 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim ds As New DataSet()
ds.ReadXml(Request.MapPath("try.xml"))
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
ViewState("dt") = ds.Tables(0)
End If
End Sub
''' <summary>
''' Handle the Edit event of GridView for assigning the specific row to be
''' in the edit mode.
''' </summary>
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = DirectCast(ViewState("dt"), DataTable)
GridView1.DataBind()
End Sub
''' <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 Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
For i As Integer = 1 To GridView1.Rows(e.RowIndex).Cells.Count - 1
dt.Rows(e.RowIndex)(i - 1) = TryCast(GridView1.Rows(e.RowIndex).Cells(i).Controls(0), TextBox).Text
Next
dt.AcceptChanges()
GridView1.EditIndex = -1
GridView1.DataSource = dt
GridView1.DataBind()
dt.WriteXml(Request.MapPath("try.xml"))
End Sub
''' <summary>
''' Cancel edit and set the mode of the GridView to normal viewing mode.
''' </summary>
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
''' <summary>
''' Insert the data into the DataTable, re-write into the xml file and
''' re-databind to the GridView.
''' </summary>
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
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()
End Sub
''' <summary>
''' Delete the row from DataTable and write data into xml file,
''' re-databind to the GridView.
''' </summary>
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)
dt.Rows.RemoveAt(e.RowIndex)
dt.WriteXml(Request.MapPath("try.xml"))
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
End Class
deongee
Member
21 Points
128 Posts
Re: Visual basc and XML data
May 04, 2012 02:20 PM|LINK
Hi Decker,
I would like a free sample code for this. Thanks
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Visual basc and XML data
May 05, 2012 12:33 AM|LINK
No problem but please leave your Email and I'll send it to you within 5 days……
Reguards!
deongee
Member
21 Points
128 Posts
Re: Visual basc and XML data
May 05, 2012 02:15 AM|LINK
my email is deongee@hotmail.com
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Visual basc and XML data
May 05, 2012 05:16 AM|LINK
OK,send you within 5 days……