ASP. NET + XML creates a message board (learning XSLT Technology)

Source: Internet
Author: User
Tags xsl xsl file xslt

I. Overview:

As an important part of a website, the guestbook is a place for visitors to express their opinions. It is also a powerful tool for website administrators to understand the basic operation of the website, therefore, the message book plays a very important role in the current website.

However, it was not easy to develop a message book before, and developers often had a lot of work. Now, with the release of Microsoft vs. net, the corresponding technologies are also updated. In particular, XML technology is widely used in. NET Framework, which makes the entire. NET architecture have a very favorable foundation. The new programming model introduced in ASP. NET makes it easier to develop Web applications. This article introduces how to create a message book based on the advantages of ASP. NET and XML.

II. Implementation Method:

A basic guestbook should have at least two functions: accepting user input information and saving the information to the background database; Displaying user input information. User input information generally includes user name, email address, QQ number, user homepage, and message information. This information is usually stored in a table in the background database, however, this article uses an XML file to store this information. When displaying user input information, you usually need to display all information. The method here is to read data from the XML file and use XSLT technology to convert the format, finally, it is displayed in the browser as HTML.

In this way, our message book requires two web pages, one for receiving user input information, and the other for displaying user input information. The XML file (guestbook. XML) that stores information must have the following structure:

<? XML version = "1.0" encoding = "gb2312"?>
<Guestbook>
<Guest>
<Name> ling huchong </Name>
<Email> doose@etang.com </Email>
<QQ> 10102350 </QQ>
<Homepage> www.doose.com <Comment> This message book was created by "Ling Hu Chong". I hope you like it. :) if you want to know how to create a message book of your own, Please carefully read "Using ASP. net and XML technology to create a message book! </Comment>
</Guest>
</Guestbook>

Next we will first create a web page-guestbook. aspx for receiving user input information. According to the above basic requirements, the web page consists of the following parts: Message book title, "User name:" tag and input box, "email address:" tag and input box, "QQ number: "tag and input box," personal homepage: "tag and input box," message: "tag and input box, a" OK "button, a" reset "button, and a" view Guestbook "button. The page also contains two verification buttons, it is used to verify whether the user name and email address are empty. If it is empty, the user is reminded to enter the email address. At the same time, in order to make the message book have a good user interface, I used tables for page layout, so that the various components in the message book can be properly organized and hierarchical. For the detailed code on this web page, refer to the source code that is attached to the document, which is not provided here. The following figure shows the page layout:
 

Figure 1

After finishing the layout of the web page, we just completed a part of the work, so far we have not performed real encoding. I think you must understand or be familiar with the post-code technology in ASP. NET. It separates the layout of the web page from the back-end coding work area, achieving a good separation effect. Next, we will compile the corresponding message functions for the three buttons on the web page:

Private void btnok_click (Object sender, system. eventargs E)
{
Savexmldata ();

Name. Text = "";
Email. Text = "";
QQ. Text = "";
Homepage. Text = "";
Comment. Text = "";
}

Private void btnreset_click (Object sender, system. eventargs E)
{
Name. Text = "";
Email. Text = "";
QQ. Text = "";
Homepage. Text = "";
Comment. Text = "";
}

Private void btnview_click (Object sender, system. eventargs E)
{
// Display the message of all users
Response. Redirect ("viewguestbook. aspx ");
}

Among them, the first button is the most important. It can store user input information in an XML file. The called method is savexmldata (); the second button only resets and clears the text box. The third button displays all user input information on another web page. At the same time, after successfully saving the information, the first button will also direct the browser to the page displaying all user input information.
Next we will analyze the savexmldata () method in detail, and its implementation is as follows:

Private void savexmldata ()
{
Try
{
// Create an xmldocument object to load the XML file storing information
Xmldocument xdoc = new xmldocument ();
Xdoc. Load (server. mappath ("guestbook. xml "));

// Create a new guest node and add it to the root node
Xmlelement parentnode = xdoc. createelement ("guest ");
Xdoc. documentelement. prependchild (parentnode );

 

// Create all nodes used to store information
Xmlelement namenode = xdoc. createelement ("name ");
Xmlelement emailnode = xdoc. createelement ("email ");
Xmlelement qqnode = xdoc. createelement ("QQ ");
Xmlelement homepagenode = xdoc. createelement ("Homepage ");
Xmlelement commentnode = xdoc. createelement ("comment ");

// Obtain text information
Xmltext nametext = xdoc. createtextnode (name. Text );
Xmltext emailtext = xdoc. createtextnode (email. Text );
Xmltext qqtext = xdoc. createtextnode (qq. Text );
Xmltext homepagetext = xdoc. createtextnode (homepage. Text );
Xmltext commenttext = xdoc. createtextnode (comment. Text );

// Add the nodes for each storage information created above to the guest node, but it does not contain the final value
Parentnode. appendchild (namenode );
Parentnode. appendchild (emailnode );
Parentnode. appendchild (qqnode );
Parentnode. appendchild (homepagenode );
Parentnode. appendchild (commentnode );

// Add the obtained text information to the corresponding node
Namenode. appendchild (nametext );
Emailnode. appendchild (emailtext );
Qqnode. appendchild (qqtext );
Homepagenode. appendchild (homepagetext );
Commentnode. appendchild (commenttext );

// XML file for saving storage Information
Xdoc. Save (server. mappath ("guestbook. xml "));

// Display the message of all users
Response. Redirect ("viewguestbook. aspx ");
}
Catch (exception e ){}
}

This method mainly uses the xmldocument class, xmlelement class, And xmltext class. These classes are included in system. in the XML namespace. Therefore, add using system at the beginning of the code file. XML statement. This method uses a try-Catch Block. In the try part, you first create an xmldocument object to load the XML file, create the son-Guest node of the root node and add the five sub-nodes required to store information under the guest node. All these subnodes are xmlelement objects, which are obtained through the createelement () method of the xmldocument object. At the same time, the xmldocument object also obtains text information through the createtextnode () method and adds it to the corresponding node later. After reasonably adding the guest node, its subnodes, and text information, the xmldocument object saves user input information to the XML file through the SAVE () method. Finally, the browser directs to the page where all user input information is displayed. In this way, the web page runs in effect 2:
 

Figure 2

Next we will create the page for displaying all user input information-viewguestbook. aspx. On this web page, we need to use XSLT technology to display the data in the XML file created earlier in HTML format. Because XSLT technology is used to display user input information, we do not need to add any Web controls when designing the web page. We only need to reload the load () method of the web page.

Private void page_load (Object sender, system. eventargs E)
{
// Create an xmldocument object to load the XML file storing information
Xmldocument xdoc = new xmldocument ();
Xdoc. Load (server. mappath ("guestbook. xml "));

// Create a terratransform object and import the XSL File
Transform transform XSLT = new transform ();
XSLT. Load (server. mappath ("guestbook. XSL "));

String xmlquery = "// Guestbook ";
Xmlnodelist nodelist = xdoc. documentelement. selectnodes (xmlquery );

Memorystream MS = new memorystream ();
XSLT. Transform (xdoc, null, MS );
Ms. Seek (0, seekorigin. Begin );

Streamreader sr = new streamreader (MS );

// Display the output result
Response. Write (Sr. readtoend ());
}

This method first creates an xmldocument object to load the XML data file created earlier, then creates an external transform object and imports the corresponding XSL file. The content in the XSL file can be used to format the data in the original XML file into HTML and display it in the browser. Because XSLT conversion is used, we have to add the using system. xml. XSL statement at the beginning of the code file.

The following is the source code of the XSL file. The most important part is <XSL: template match = "name"> ...... </XSL: Template>.

<XSL: stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<XSL: template match = "/">
<Table border = "1" style = "border-collapse: collapse" bordercolor = "teal" align = "center" width = "505" Height = "34">
<Tr>
<TD valign = "Middle" align = "center" bgcolor = "teal" colspan = "2" width = "505" Height = "85">
<Font style = "color: White; Background-color: teal; font-family: 文; font-size: X-large; font-weight: bold; "> welcome to the" Linghu Chong "Guestbook! </Font>
</TD>
</Tr>
<Tr> <TD width = "505" Height = "26" align = "Left" colspan = "2"> </TD> </tr>
<XSL: For-each select = "// guest">
<XSL: Apply-templates select = "name"/>
</XSL: For-each>
<Tr>
<TD valign = "Middle" align = "center" colspan = "2" width = "505">
<Font>
This guestbook was developed by <a href = "mailto: 0024108@fudan.edu.cn"> Wang Kaiming </a>! </Font>
</TD>
</Tr>
</Table>
</XSL: Template>
<XSL: template match = "name">
<Tr>
<TD width = "95" Height = "26" align = "right">
<Font> User name: </font>
</TD>
<TD width = "400" Height = "26" valign = "Middle" align = "Left">
<Font> <XSL: value-of select = '.'/> </font>
</TD>
</Tr>
<Tr>
<TD width = "95" Height = "26" align = "right" bgcolor = "e0e0e0">
<Font> email address: </font>
</TD>
<TD width = "400" Height = "26" valign = "Middle" align = "Left" bgcolor = "# e0e0e0">
<Font> <a href = "mailto :{.. /Email} "> <XSL: Apply-templates select = ".. /email "/> </a> </font>
</TD>
</Tr>
<Tr>
<TD width = "95" Height = "26" align = "right">
<Font> QQ number: </font>
</TD>
<TD width = "400" Height = "26" valign = "Middle" align = "Left">
<Font> <XSL: Apply-templates select = "../QQ"/> </font>
</TD>
</Tr>
<Tr>
<TD width = "95" Height = "26" align = "right" bgcolor = "# e0e0e0">
<Font> personal homepage: </font>
</TD>
<TD width = "400" Height = "26" valign = "Middle" align = "Left" bgcolor = "# e0e0e0">
<Font> <a href = "http ://{.. /homepage} "target =" _ blank "> <XSL: Apply-templates select = ".. /Homepage "/> </a> </font>
</TD>
</Tr>
<Tr>
<TD width = "95" Height = "26" valign = "TOP" align = "right">
<Font> message: </font>
</TD>
<TD width = "400" Height = "26" valign = "TOP" align = "Left">
<Font> <XSL: Apply-templates select = "../comment"/> </font>
</TD>
</Tr>
<Tr> <TD width = "505" Height = "26" align = "Left" colspan = "2"> </TD> </tr>
</XSL: Template>
</XSL: stylesheet>

In this way, when a user clicks the "view Guestbook" button or successfully enters the information, the browser directs to the web page that displays all user input information. The running result is as follows:
 

Figure 3

Iii. Summary:

In this way, a message book with basic functions is completed, from which we can understand the application of ASP. NET technology is quite easy to develop Web applications. At the same time, combined with XML technology, Asp. net can become more powerful. The XSLT technology introduced in the article is very useful. You can refer to other related documents to make it a powerful tool in your development process.

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.