Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Building XML Applications with the .NET Framework 2.0 : Page 3

XML is the most popular technology for structuring data. .NET 2.0 provides much needed functionality to enable one to build XML-based applications. The enhanced DataTable now supports reading and writing XML data, which is crucial in creating XML-enabled applications.


advertisement
Writing Code for the Application
There are two things that you need write code for in order to complete this form: loading the form with data and enabling the user to add/edit/delete/save the data.

Loading the form with data
As of now physical XML files are not available, however you have a dataset that contains two XML DataTables. The first time the application runs you need to write code to create the XML file. The next time the application runs it needs to check for the existence of both the files and then load the file. Double-click the form and add the following code to the form load event:

If CheckFileExistence() Then LoadDataSet() Else 'Application is running for first time, hence XML files are missing 'create XML files based on DataSet Schema CreateXMLFile() End If

Next you need to define a few variables indicating the file names and their locations. Add the following definitions to the class file above the code for the form load event:


Dim myDocumentsFolder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments Dim activitiesFileName As String = "\Activities.xml" Dim tasksFileName As String = "\Tasks.xml" Public activitiesFile As String = String.Concat(myDocumentsFolder, activitiesFileName) Public tasksFile As String = String.Concat(myDocumentsFolder, tasksFileName)

Here I used the new My keyword to get the folder path for the MyDocuments folder, where I store the application files. I have defined variables to store the names of the activities and tasks XML files. You need to write code for the functions used in the form load event above, namely: CheckFileExistence, CreateXMLFile, and LoadDataSet. Add the following three code snippets to the form:

Public Function CheckFileExistence() As Boolean If My.Computer.FileSystem.FileExists(activitiesFile) And _ My.Computer.FileSystem.FileExists(tasksFile) Then Return True Else Return False End If End Function

Here again, I'm using the My keyword, to easily access the FileExists function to check for both the XML files. Alternatively, you could use the new snippet functionality to add standard file look-up code to your application, by right-clicking anywhere in code view.

Next you want to add some sample data to the tasks table and auto-generate the schema in the XML file:

Public Sub CreateXMLFile() 'Start adding sample data to Master Tasks Table DsActivitiesTasks.Tasks.AddTasksRow("Email") DsActivitiesTasks.Tasks.AddTasksRow("Browsing") 'End adding sample data to Master Tasks Table 'Write XML file and inline schema DsActivitiesTasks.Tasks.WriteXml(tasksFile, System.Data.XmlWriteMode.WriteSchema) DsActivitiesTasks.activities.WriteXml(activitiesFile, System.Data.XmlWriteMode.WriteSchema) End Sub

The CreateXMLFile function allows me to add some sample data to the tasks table. This enables my tasks drop-down in the form to be populated. The next two lines of code are extremely important. Here I'm using the new functionality in the DataTable to write XML, however I'm also passing an important parameter that auto-generates the schema within the XML file. The schema is important as it specifies the data type and constraints of the various columns as well as the relations between the tables.

When you finish coding the application it will be a good exercise to examine the generated schema and XML data as you will realize how the functionality in the DataTable's WriteXML method saves you a ton of work.

The last bit of code, with regards to loading the application, is to read the data from the XML files and load it into the dataset.

Public Sub LoadDataSet() DsActivitiesTasks.tasks.ReadXml(tasksFile) DsActivitiesTasks.activities.ReadXml(activitiesFile) End Sub

.NET Framework 1.x did not allow you to load multiple XML files into the dataset at one go. However the enhanced features in the DataTable enable you to easily load both the tasks and the activities XML files that have been just created.

Saving and Deleting Data
The DataGridView automatically provides the user a facility to add new data by moving to a new row; similarly for editing the data, the user just moves to the particular row or column and edits the data. Providing functionality to save and delete data is what you need to write code for. Saving data is a simple process of accepting changes made by the user and then using the WriteXML function of the DataTable to write the XML data and the schema back to the activities file. Similarly, deleting data involves capturing the current row and deleting that row. Add the following two functions, which handle the btnSave and btnDelete click events.

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnSave.Click Me.DsActivitiesTasks.activities.AcceptChanges() Me.DsActivitiesTasks.activities.WriteXml(activitiesFile, System.Data.XmlWriteMode.WriteSchema) End Sub Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnDelete.Click Me.DataGridView1.Rows.RemoveAt(Me.DataGridView1.CurrentRow.Index) btnSave_Click(sender, e) End Sub

Adding and Editing Tasks
Figure 6. UI for Form EditAddTasks: In this dialog, you can see the layout of the DataGridView and the two buttons for the EditAddTasks form.
You still need to write code for the button, btnEditAddTasks, which will allow the user to specify different kinds of tasks in the tasks.xml file. I will get back to this in a bit, as you need to add a new form to the project to enable this. Add a new form and name it frmEditAddTasks.

Drop a DataGridView and two buttons on the new form. Name the buttons btnSave and btnDelete and layout the form UI as shown in Figure 6.

Add the following code to the load event of the form:

DataGridView1.DataSource = frmActivities.DsActivitiesTasks DataGridView1.DataMember = frmActivities.DsActivitiesTasks.tasks.TableName

It is important to note that I'm referring to the instance of the DataSet, dsActivitiesTasks, which is present in the form frmActivities. Similarly, I have specified the DataMember to be the name of the tasks table. This is to ensure that in both the forms the same DataSet instance is being used. This enables any changes made by the user to be reflected immediately.

Double-click the Save button on the form and add the following code to save the data changes made by the user.

frmActivities.DsActivitiesTasks.tasks.AcceptChanges() frmActivities.DsActivitiesTasks.tasks.WriteXml(frmActivities.tasksFile, _ System.Data.XmlWriteMode.WriteSchema)

Loading the EditAddTasks Form
Figure 7. Completed Application: The running application lets you add new tasks to your task list, complete with the drop-down list of various tasks to select from.
The last thing remaining to do is to load the frmEditAddTasks form when the button btnEditAddTasks, on the frmActivities form is clicked. Add the following code to the click event of the btnEditAddTasks.

Dim frmtasks As New frmEditAddTasks frmtasks.ShowDialog()

Running the Application
Hit F5 to run the application. Because the application is running for the first time, the XML files will be generated and sample data will be added to the Tasks XML file. Click the "Edit/Add Tasks" button on the form, which loads the frmEditAddTasks form. You will see the sample data. Add new task types if you require. Clicking the save button writes the data to XML. Close the frmEditAddTasks. On the frmActivities enter a new activity. The Taskid field drops down to show the list of different kind of tasks (see Figure 7).

This application definitely needs a lot more in terms of functionality, such as error checking, validation, etc. My goal, however, has been to show how easy it is to build applications handling multiple XML files as a data store.

The enhanced DataTable class is now fully XML enabled; it eases the pain of reading and writing XML files directly by enabling multiple XML files to be loaded in to a dataset. This, in combination with the DataGridView control, enables one to easily build XML-enabled applications.



Sanjay Shetty is the CEO of Wireless Strategist & Consultants, a consultancy for mobility, design, architecture, and Microsoft.NET. He is the Microsoft Regional Director in Mumbai, India.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap