XML-based application created in ASP. net2.0

Source: Internet
Author: User

XML is a widely used technology on the Web. In. NET 2.0, XML provides more abundant functions, making it easier to create XML applications. In this article, we will give an example to illustrate some new features of. NET 2.0 when creating an XML application.

First, we usually choose to store data in the form of a database when creating a web application. However, to store data in plain XML, this is indeed a challenge. In the. NET 1.0/1.1 framework, the support for XML is limited, for example, the support for multiple XML files in the same application. In. NET 2.0, this situation has been changed and new features have been added, such:

1. datatable can now read and write XML data

2. The datgridview control allows you to display data from different tables in dataset.

The following is an example of a Web application to describe these new features. In this program, I store all the data in the XML file. Demonstrate how to read and write multiple XML files in dataset at the same time, and how to bind an XML file to the datagridview control; it also shows how the datagridview control reads and writes data from XML files and how to find data in multiple XML files. These new features are previously not available in. net.

  Task tracking application

Next, we will create a web application that implements "task tracking. This application is actually very simple and can help you track the progress of some tasks and activities. It needs to create two XML files: activities. xml and tasks. xml. The activites File Stores daily activities, including the tasks, descriptions, start times, and end times of each activity. The tasks file stores the task type, such as browsing email, programming, and reading books. Displays the data structure between the two files.
   

 

In general, you may want to manually create these two XML files, but this is not really advocate, because in. in ne 2005, you can use the Dataset Designer To Help You design the structure of the XML file and allow you to create an XML file of the link. The following is a detailed explanation.

1) Open visual.net 2005 beta 2, create a Windows application, select the VB.net language, name the project dtkxml, add a dataset file, and name it dsactivitiestasks .. Add two tables, activities and tasks (see figure 1 ). You only need to right-click the designer interface and choose add-> datatable from the shortcut menu.

Next, right-click the table name of each table and select Add-> column in the pop-up menu to add columns for each table, the following table lists the attributes and data types:
  
Activites table

Column Name Property
1 Activityid Datatype = system. int32
Autoincrement = true
Autoincrementseed = 1
Autoincrementstep = 1
Unique = true
2 Taskid Datatype = system. int32
3 Description Datatype = system. String
4 Startime Datatype = system. datetime
5 Endtime Datatype = system. datetime

Tasks table

Column Name Property
1 Taskid Datatype = system. int32
Autoincrement = true
Autoincrementseed = 1
Autoincrementstep = 1
Unique = true
Allowdbnull = false
2 Task Datatype = system. String
Allowdbnull = false

 
Next, it is very simple to associate the two XML files. In the design view of activities and task tables, right-click and select "add relation" from the pop-up menu. Set the associations between the two files as shown in.

2) at this time, we can design the application interface. An application consists of two forms. The first form allows you to process information about the activity. The second form allows you to edit various types of tasks.

Rename the default form in the project to frmactivities, and drag a dview control to the form in the toolbox. Next, select the Smart Tag tag in the upper-right corner of the control. A menu appears, as shown in. Select dsactivitiestasks (the dataset type file we just created) as the data source of the datagridview.

At this time, in the lower part of the form component tray, you will find that Visual Studio has added the dsactivitiestasks instance, and another component is dsactvitiestasksbindingsource. In fact, you don't need to bind the source, you just need to bind the dataset directly to the datagridview control. Therefore, select the bingding source control and delete it. Then, use the smarttag function again and select dsactivitiestasks as the data source again.

Next, we need to set the properties of the datagridview control to hide some columns that do not need to be displayed. When setting the control, we select the smarttag flag again, in the pop-up menu, select "Edit columns" and set its attributes according to the following table,

Column Property
Activityid Visible = false
Taskid Columntype = maid
Datasource = dsactivitiestasks
Datamember = tasks. Task
Valuemember = tasks. taskid
Description Width = 180

Add three buttons to the form. The names are btneditaddtasks, btnsave, and btndelete. Then, the interface of the first form is set.

  Write related Code

When writing code, there are two steps: 1. Load the relevant data; 2. enable the user to add, delete, and save the data. The steps below are described as follows:

Load related data to the datagridview control.

Because we already have dataset files, we can create these two files. During the second running of the application, we will first check whether these two XML files exist. The Code is as follows:

Write Data in the form_load event

If checkfileexistence () then
Loaddataset ()
Else
'If the two XML files do not exist, create these two XML files.
Createxmlfile ()
End if

Next, define a series of variables to store the specific location of the Two XML files, as shown in the following code:

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)

Note the usage of my. Computer. filesystem. specialdirectories. mydocuments to return the specific location of "My Documents" on the current computer. Here, save the two files to "my documents. The activitiesfile and tasksfile variables are used to save the actual access paths of the Two XML files. The following is the Code related to several processes:

Public Function checkfileexistence () as Boolean
'Check whether two XML files exist
If my. Computer. filesystem. fileexists (activitiesfile) and _
My. Computer. filesystem. fileexists (tasksfile) then
Return true
Else
Return false
End if
End Function

Public sub createxmlfile ()
Dsactivitiestasks. Tasks. addtasksrow ("email ")
Dsactivitiestasks. Tasks. addtasksrow ("browsing ")
Dsactivitiestasks. Tasks. writexml (tasksfile, system. Data. xmlwritemode. writeschema)
Dsactivitiestasks. Activities. writexml (activitiesfile, system. Data. xmlwritemode. writeschema)
End sub

In the above createxmlfile process, the first two sentences of code first add some sample data to the XML file of the task, namely email and browsing. the following two codes are. the newly added function in net 2.0 sets an important parameter writeschema to automatically save the data to the tasksfile and activitiesfile files, and automatically saves the XML Schema mode, only in this way can we save the relationship between two files on the data table.

Public sub loaddataset ()
Dsactivitiestasks. Tasks. readxml (tasksfile)
Dsactivitiestasks. Activities. readxml (activitiesfile)
End sub

The above is the loaddataset process, which is very simple and reads Two XML files respectively. As you can see, in. NET 2.0, multiple XML files can be loaded in one dataset at the same time, as shown in the preceding figure. In. NET 1.1, this is very difficult.

  Save and delete data

In the datagridview datagri, you can automatically add records. You only need to move the cursor to a new row. When you edit the data, you need to save it. At this time, you need to write a little code. You only need to call the writexml method to save the data. Similarly, you need to call the removeat method to delete the data. As follows:

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. Maid. removeat (Me. Maid. currentrow. Index)
Btnsave_click (sender, E)
End sub

  Add and edit tasks

We need to write the relevant code for the btneditaddtasks button, and we create a new form frmeditaddtasks. In this form, we can add and delete different tasks. First, drag a dview control and add two buttons named btnsave and btndelete, as shown in:

Write the following code in the load event:

Datagridview1.datasource = frmactivities. dsactivitiestasks
Datagridview1.datamember = frmactivities. dsactivitiestasks. Tasks. tablename

To bind the dview to the tasks file of the dataset. Note that it is called in the form of frmactivities. dsactivitiestasks, because the dataset is set up in frmactivities In the first form.

Then, write the code for the Save button:

Frmactivities. dsactivitiestasks. Tasks. acceptchanges ()
Frmactivities. dsactivitiestasks. Tasks. writexml (frmactivities. tasksfile ,_
System. Data. xmlwritemode. writeschema)

The code for deleting the delete button is also simple:

Private sub btndelete_click (byval sender as system. Object, byval e as system. eventargs) handles btndelete. Click
Me. Maid. removeat (Me. Maid. currentrow. Index)
Btnsave_click (sender, E)
End sub

Finally, remember to write the following code in the btneditaddtasks button of the frmactivities form so that when you click this button, the task editing window will pop up.

Dim frmtasks as new frmeditaddtasks
Frmtasks. showdialog ()

  Run code

Now you can start running the code. The result is shown in:

Click "Edit/Add tasks" to add different tasks. You can also select different tasks in each activity from the drop-down list in this form, specify the description, start time, and end time of each task, and save and delete them, and all of this is through the use. NET 2.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.