One of the great features of .NET is DataSets, DataTables, and DataRows, and using them to work with different types of data (like tables from a SQL Server, or an XML document) in a similar fashion. This article focuses on editing, and saving a simple XML document with a DataGrid. Users should already be familiar working with DataGrids, and displaying XML within a DataGrid from previous articles.
First, let's take a look at our XML document. It's just a simple article list with an article tag for each article, and a tag for id, author, title, and text
<?xml version="1.0" standalone="yes"?>
<articlelist>
<article>
<id>1</id>
<title>The hills are alive</title>
<author>Blarney Stone</author>
<articletext>The hills were alive....</articletext>
</article>
<article>
<id>2</id>
<title>The mobsters of the deep!</title>
<author>Blarney Stone</author>
<articletext>They were mobsters, and they were from the deep....</articletext>
</article>
<article>
<id>3</id>
<title>The secret of the gates!</title>
<author>Blarney Stone Jr. III</author>
<articletext>A long time ago in a land faraway deep in the city....</articletext>
</article>
<article>
<id>4</id>
<title>The canopener</title>
<author>Blarney Stone Jr. V</author>
<articletext>A long time ago in a land faraway....</articletext>
</article>
</articlelist>
|
As a quick note, the id field is not used in the editing process, but is there to make relations to other XML documents in the future.
When working with our XML document in the DataGrid we display rows and access DataGrid items as we normally would. The only difference is that we work closer with the DataRows and DataTables from our source file in order to edit and save the new information.
Below is the full source code to edit our file.
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data"%>
<script runat="server">
private DataSet CreateDataSource(){
XmlDataDocument mydata = new XmlDataDocument();
mydata.DataSet.ReadXml(Server.MapPath("xml_data_x1.xml"));
return mydata.DataSet;
}
public void Page_Load(Object sender, EventArgs e){
if (!IsPostBack) {
mygrid.DataSource = CreateDataSource();
mygrid.DataBind();
}
}
public void doCancel(Object sender, DataGridCommandEventArgs e)
{
mygrid.EditItemIndex = -1;
mygrid.DataSource = CreateDataSource();
mygrid.DataBind();
}
public void doEdit (Object sender, DataGridCommandEventArgs e)
{
mygrid.EditItemIndex = e.Item.ItemIndex;
mygrid.DataSource = CreateDataSource();
mygrid.DataBind();
}
public void doUpdate(Object sender, DataGridCommandEventArgs e)
{
string Author = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string Title = ((TextBox)e.Item.Cells[2].Controls[1]).Text;
string ArticleText = ((TextBox)e.Item.Cells[3].Controls[1]).Text;
mygrid.EditItemIndex = -1;
DataSet ds = CreateDataSource();
DataRow row = ds.Tables[0].Rows[e.Item.ItemIndex];
row["author"]=Author;
row["title"]=Title;
row["articletext"]=ArticleText;
ds.WriteXml(Server.MapPath("xml_data_x1.xml"));
mygrid.DataSource = CreateDataSource();
mygrid.DataBind();
}
public void doDelete(Object sender, DataGridCommandEventArgs e)
{
mygrid.EditItemIndex = -1;
DataSet ds = CreateDataSource();
DataRow row = ds.Tables[0].Rows[e.Item.ItemIndex];
row.Delete();
ds.WriteXml(Server.MapPath("xml_data_x1.xml"));
mygrid.DataSource = CreateDataSource();
mygrid.DataBind();
}
</script>
<center>
<form runat="server">
<asp:datagrid runat="server" AutoGenerateColumns="false"
width="450" id="mygrid"
OnEditCommand="doEdit"
OnCancelCommand="doCancel"
OnUpdateCommand="doUpdate"
OnDeleteCommand="doDelete">
<HeaderStyle BorderColor="White" BackColor="black"
ForeColor="White"
Font-Bold="True"
Font-Name="Arial"
Font-Size="9" HorizontalAlign="Center"/>
<ItemStyle BorderColor=""
BackColor="#FFFFF0"
ForeColor="Black"
Font-Name="Arial"
Font-Size="8"
Font-Bold="False" HorizontalAlign="Center"/>
<EditItemStyle BorderColor=""
BackColor="#FFFFF0"
ForeColor="Black"
Font-Name="Arial"
Font-Size="7"
Font-Bold="False" HorizontalAlign="Center"/>
<Columns>
<asp:BoundColumn HeaderText="ID" ReadOnly="true" DataField="id"/>
<asp:TemplateColumn>
<HeaderTemplate>
<b> Author </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "author") %>'
runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Author"
textMode="MultiLine" Columns="35"
Rows="8" Text='<%# DataBinder.Eval(Container.DataItem, "author") %>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<b> Title </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'
runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="Title"
textMode="MultiLine" Columns="35"
Rows="8" size="25" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<b> Text </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "articletext") %>'
runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="ArticleText"
textMode="MultiLine" Columns="35"
Rows="8" Text='<%# DataBinder.Eval(Container.DataItem, "articletext") %>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn
ButtonType="LinkButton"
CancelText="Cancel"
EditText="Edit"
UpdateText="Update" />
<asp:ButtonColumn Text= "Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
</form>
|
Let's look at what we are doing in more detail.