Using ASP.net to implement XML based guest Book

Source: Internet
Author: User
Tags add object comments datetime implement net string tostring
Asp.net|xml

A Profile:

The guest book is an important part of the website, is the place that the visitor comments, is also the website administrator to understand the website basic situation powerful tool. So the guest book plays an important role in today's website.

But it's not easy to develop a guest book in the past, and developers often have a lot of work to do. And now with the introduction of Microsoft Vs.net, the corresponding technology innovation. In particular, the extensive use of XML in the. NET Framework makes the entire. NET Framework extremely advantageous. Vs.net launched in the same time, but also accompanied by a new language C #. As an important part of Microsoft. NET strategy, C # has exceptional performance. So the ASP.net program in this paper is described in C # language, and the whole program is based on XML. I used XML as a database of programs, mainly because the servers support XML files.

Two Requirements:

(1). Net SDK Beta2 and later versions

(2) Support for ASP.net Web servers

Three Description

The examples in this article are made up of two parts:

(1) guestpost.aspx-Add user information to an XML file

(2) viewguestbook.aspx-first set up a DataSet object, after the establishment, it is easy to display the data. I used a "Repeater" here to display data in a dataset. Also, readers can try to modify the viewing page according to their preferences.

Four Code:

(1) Guestpost.aspx:

<%@ Page language= "C #" enablesessionstate= "False"% >
<%@ Import namespace= "System"% >
<%@ Import namespace= "System.IO"% >
<%@ Import namespace= "System.Data"% >
<%--These are the name spaces necessary for the normal use of this procedure--% >

< HTML >
< head >
< title > Welcome to my Guest Book </title >
< script language= "C #" runat= "Server" >
< summary >
When the submit button is pressed, adjust the function
</summary >
public void Submit_click (Object sender, EventArgs e)
{
Path to the XML file where the data is saved
If your path differs from the following, please modify it
string datafile = "Db/guest.xml";

Use a try-catch block to complete the information add function
try{
Deal with the page only when it is valid
if (page.isvalid) {

Errmess. Text= "";
Open a FileStream in read mode to access the database
FileStream fin;
fin= New FileStream (Server.MapPath (datafile), FileMode.Open,
Fileaccess.read,fileshare.readwrite);
Create a Database object
DataSet Guestdata = new DataSet ();
Read XML Schema from database only
Guestdata.readxmlschema (Fin);
Fin. Close ();
Create a new data row from the schema of a dataset
DataRow NewRow = guestdata.tables[0]. NewRow ();
Fill in data rows with corresponding values
newrow["Name"]=name.text;
newrow["Country"]=country.text;
newrow["Email"]=email.text;
newrow["Comments"]=comments.text;
newrow["DateTime"]=datetime.now.tostring ();
Complete and add data rows to the dataset
Guestdata.tables[0]. Rows.Add (NewRow);
Create a new FileStream for the database file with another write mode and save the file
FileStream Fout;
Fout = new FileStream (Server.MapPath (datafile), FileMode.Open,
Fileaccess.write,fileshare.readwrite);


Guestdata.writexml (Fout, Xmlwritemode.writeschema);
Fout. Close ();
Hide the current panel
Formpanel.visible=false;
Display a panel with thank-you information
Thankpanel.visible=true;
}
}
catch (Exception edd)
{
Catching exceptions
Errmess. text= "Error writing XML file, Reason:" +edd. ToString ();
}
}
</script >
< LINK href= "mystyle.css" Type=text/css rel=stylesheet >

(2) Viewguestbook.aspx:

<%@ Page language= "C #"% >
<%@ Import namespace= "System"% >
<%@ Import namespace= "System.IO"% >
<%@ Import namespace= "System.Data"% >
<%--above is the required name space--% >

< HTML >
< head >
< title > Welcome to my Guest Book </title >
< script language= "C #" Runat=server >
When the page is downloaded, run this script
public void Page_Load (Object sender, EventArgs e)
{
The path to the XML file that contains all the data
If your path differs from the following, please modify the
string datafile = "Db/guest.xml";

Using a try-catch block to complete the information reading function
Try
{
Set up a DataSet object
DataSet Guestdata = new DataSet ();
Open a FileStream for the database file
FileStream fin;
Fin = new FileStream (Server.MapPath (datafile), FileMode.Open,
Fileaccess.read,fileshare.readwrite);
Read the contents of the database to the dataset
Guestdata.readxml (Fin);
Fin. Close ();
Pay the dataset in the first table to repeater
Mydatalist.datasource = Guestdata.tables[0]. DefaultView;
Mydatalist.databind ();
}
catch (Exception edd)
{
Catching exceptions
Errmess. Text= "cannot read data from an XML file, Reason:" +edd. ToString ();
}
}
</script >
< LINK href= "mystyle.css" Type=text/css rel=stylesheet >

Five Summarize:

This program is complete, I believe you have a general understanding of the principle of implementation and skills. Through this article, I want to show you the advantages of using XML to deal with some database problems: The server supports XML files, and the process is fairly straightforward. Of course, XML also has its drawbacks, that is, when the database is very large, the parsing process will take a long time, so still need to adopt a large database system. So, I just want to use this article to play a role.



Related Article

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.