Quickly Editing an XML File, Part 2By Zubair Ahmed and Scott Mitchell
In Part 1 we looked at the
Page_Load event handler and the
LoadXML()method, as well as the DataGrid's declaration. In this final part we'll look at the DataGrid's
event handlers and see how everything is tied together.
Examining the DataGrid's Event Handlers
Since the Delete button's
CommandNamehas been set to Delete whenever the Delete button is pressed, the DataGrid's
DeleteCommandevent is fired upon postback. Since in the DataGrid's declaration we wired up this event to the
DelXMLwill execute whenever this button is pressed.
I have decided that if the DataGrid is in edit mode then the user cannot delete a record until canceling out of
edit mode. Therefore, in the
DelXML event handler, I first check that the DataGrid is not in the
edit mode (by making sure
EditItemIndex = -1). If it is not,
Else segment gets executed, which displays a Label Web control mentioning the error. If there
is no row being edited, though, I make sure the error Label is hidden.
In addition, I reference the DataGrid's
DataKeys collection to obtain the primary key value of
the DataGrid row that is being deleted.
Next the XML file
sample.xml is read into a DataSet and the record is located and deleted.
The above code deletes the record from the XML file by the following steps:
- First, the XML data is read into a DataSet
- Next, the DataSet's DataView's
RowFilterproperty is used to filter the results down to the one record whose primary key matches the primary key of the record whose Delete button was clicked
- The singled out record is deleted from the DataSet
RowFilterproperty is reset
- The contents of the XML file are written back to disk, overwriting the previous version of the XML file
When the Add LinkButton is clicked, the DataGrid's
ItemCommand event is fired and the associated event
doInsert - executes.
doInsert, as its name suggests, inserts a new record into
the XML document. It makes a couple of checks to ensure that the person is not entering a record with a duplicate primary
key value. Below is a shorted version of the
doInsert method - some code has been removed for brevity.
This code reads in the values from the TextBox Web controls in the footer, ensures that there does not already exist
a record with the same primary key field value, adds the record to the DataSet, and then writes the DataSet's contents
back out to an XML file. After doing this, the
LoadXML() method is called to rebind the new XML data
to the DataGrid.
The last final piece of the puzzle - providing editing support, works in a similar manner to the adding a new record feature. Namely, when the edited row's Update button is clicked, the event handler picks out the edited values of the edited row, reads in the XML file into a DataSet, locates the record that was updated, updates the fields, and then writes the XML file back to disk. Due to its similarity to inserting a record, the event handlers for editing a record are not discussed here - you can check them out in the code download at the end of this article. (For some background reading on creating an editable DataGrid, read: An Extensive Examination of the DataGrid Web Control: Part 6.)
|Changes Required For Different XML Files|
|If you are using a different XML file, you will need to change the insert and update event handlers. Specifically, you will need to change it so that they read from the appropriate TextBox Web controls and insert into/update the appropriate fields in the XML file.|
Conclusion Alright, so now you have an easy way to update, create and store your records as XML content as well as modify the data later. With the help of the DataGrid server control we were able to make a professional-looking user interface with a minimal amount of code and effort. An XML file is not a replacement for a database, however it is designed to be a supplement to a database. For simplicity, I have not included any validation controls, sorting, or paging of the DataGrid, although these could be added.
Be sure to view the live demo!