RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building XML Applications with the .NET Framework 2.0

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.

tandard applications, of course, use a database, but if you've ever investigated the idea of building an application using only XML files as a data store, you know that it has been rather challenging. The .NET Framework 1.x lacked some basic XML-enabling features, which made it difficult to build applications that can handle multiple XML files. The .NET Framework 2.0 brings with it some interesting new features and controls that make this easy:
  1. The DataTable has been enhanced to support loading and writing XML data.
  2. The DataGridView control lets you easily display data from multiple tables in a DataSet.
This article builds a simple time tracking application that exploits these new features. The application stores all of its data in multiple XML files. It explores how to read and write multiple XML files into a dataset using the new DataTable XML support (which previously wasn't possible) and how to bind XML data to the DataGridView control. It showcases how the DataGridView can be used to display and write data from an XML file and to look up data in different XML files. These latter functions required quite a bit of code in earlier versions of the framework; moreover, loading XML data into the DataTable was not possible.

TimeTracker Application
TimeTracker is a simple application I built to help me keep track of the time I spend on various activities. It requires two XML files: Activities.XML and Tasks.XML. The activities file stores the daily entries, including the tasks performed, their descriptions, and start and end times. The tasks file stores the categories for various kinds of tasks, for instance, e-mail, browsing, coding etc. Figure 1 shows the structure of these two XML files and their relationship to each other.

Author's Note: Throughout the remainder of this article, .NET refers to .NET Framework 2.0.

Figure 1. Tracking Time: The figure shows the two XML files used in the time tracker application and their data structures.
Figure 2. Setting Relations: In this dialog you set the relation between the activities and tasks tables.

XML Data Files
Normally you would be tempted to create the two XML files required for the application by hand. But there are two reasons why you shouldn't. First it's dirty and murky messing with XML, especially when you are dealing with the schema definitions and the relationships between the XML files. The second reason is you really don't need to do it manually as the DataSet designer allows you to define it visually and write out the XML files with their schema using very simple code. Unfortunately, most people aren't aware of this functionality, which has been updated with simplified terminology.

Building the Application
Fire up Visual Studio 2005 beta 2 and choose a new Windows Application template, name the project DeskTimeKeeperXML or DTKXML. Add a new file of type DataSet to the project and name it dsActivitiesTasks. You need to add two tables: activities and tasks (see Figure 1). Simply right-click the design surface and select Add->DataTable.

Similarly, you can add a column for each table by right-clicking the table name and selecting Add->Column. Properties for each column are set by selecting the column name and setting its properties in the Properties window.

Author's Note: XML is case-sensitive, hence, be careful while defining names of tables and columns.

Define the properties of the DataTable elements for each table as shown in Tables 1 and 2:

Table 1. Activites Table:
: activities

Column Name Property
1 activityid DataType=System.Int32Autoincrement=True
2 taskid DataType=System.Int32
3 description DataType=System.String
4 startime DataType=System.DateTime
5 endtime DataType=System.DateTime

Table 2. Tasks Table:
: tasks

Column Name Property
1 taskid DataType=System.Int32
2 task DataType=System.String

Relationship Between the Tables
Defining the relationship between the two DataTables is a simple matter of right-clicking the activities or task table in design view and choosing Add Relation. As shown in Figure 2 you need to change the foreign key value to taskid in the activities table. That completes the data design part of the application.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date