Asp.net+xml Create message Book

Source: Internet
Author: User
Tags empty reset xsl xsl file xslt
Asp.net|xml one. Overview:

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 Operation situation Powerful tool, therefore the guest book in the present website has played the very important role.

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 is also new. In particular, the extensive use of XML technology in the. NET framework makes the whole. NET architecture has a very advantageous foundation. And the new programming model introduced in ASP.net makes it very easy to develop Web applications. This article on the combination of ASP.net technology and the advantages of XML technology to introduce you how to create a guest book belongs to their own.

Two Implementation method:

A basic guest book should include at least two features: accept the user input and save the information to the background database, and display the information entered by the user. User input information generally includes user name, email address, QQ number, user homepage, message information, etc., this information is usually saved in a table in the background database, but this article to use an XML file to store this information. When displaying information entered by a user, it is generally necessary to display all the information, and the method is to read the data from the XML file and format it using the XSLT technology, and finally display it in the browser as HTML.

In this way, our guest book will require two Web pages, one for accepting user input and another for displaying information that the user has entered. The XML file (Guestbook.xml) that stores the information needs to have the following structure:

<?xml version= "1.0" encoding= "GB2312"?>
<guestbook>
<guest>
<name> make Fox Red </name>
<email>doose@etang.com</email>
<qq>10102350</qq>
<comment> book by "Make Fox Chong" created, I hope you can like oh: to know how to create a guest book of their own, then please read the "Use of asp.net and XML technology to create a guestbook" article! </comment>
</guest>
</guestbook>

Let's first create a Web page-guestbook.aspx to accept user input information. Based on the basic requirements mentioned earlier, the Web page includes the following sections: Guest book title, "Username:" Label and input box, "email Address:" Label and input box, "QQ Number:" Label and input box, "Personal homepage:" Label and input box, "message:" Label and input box, a " OK button, a reset button, a view Guest book button, the page also includes two validation buttons, respectively to verify the user name and email address is empty, if blank, then remind the user input. At the same time, in order to make the guest book has a good user interface, I used the table for page layout, so that the various components of the guest book can be organized, structured. For more information about the Web page, please refer to the source code that came with the article, which is not given here. The page layout is illustrated as follows:


Figure 1

Having completed the layout of the Web page, we just finished a part of the work, so far we haven't really coded it. I think we must be familiar with the code behind the ASP.net technology, it will be the layout of Web pages and back-end coding work, to achieve a good separation effect. Below we will write the corresponding function for the three buttons on the Web page separately:

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)
{
Show all user's message
Response.Redirect ("viewguestbook.aspx");
}

Where the first button is the most important, it can store the user's input information in an XML file, the method called Is Savexmldata (), and the second button only completes the text box to reset the empty work; the third button is to use another Web page to display all the user input information. At the same time, the first button, after successfully saving the information, also directs the browser to the page that displays all user input information.
Here's a detailed analysis of the Savexmldata () method, which is implemented as follows:

private void Savexmldata ()
{
Try
{
Create a XmlDocument object to load the XML file that stores the 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 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");

Get 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);

Adds the nodes of each storage information created above to the guest node but does not contain the final value
Parentnode.appendchild (Namenode);
Parentnode.appendchild (Emailnode);
Parentnode.appendchild (Qqnode);
Parentnode.appendchild (Homepagenode);
Parentnode.appendchild (Commentnode);

Add the text information obtained above to the corresponding node
Namenode.appendchild (Nametext);
Emailnode.appendchild (Emailtext);
Qqnode.appendchild (Qqtext);
Homepagenode.appendchild (Homepagetext);
Commentnode.appendchild (Commenttext);

Save an XML file that stores information
Xdoc. Save (Server.MapPath ("Guestbook.xml"));

Show all user's message
Response.Redirect ("viewguestbook.aspx");
}
catch (Exception e) {}
}

This method mainly uses the XmlDocument class, the XmlElement class, and the XmlText class, which are all contained in the System.Xml namespace, so add the statement with the using System.Xml at the beginning of the code file. The method uses a Try-catch statement block, which first loads the XML file by creating a XmlDocument object in the Try section, then creates the son-guest node of the root node and adds the five child nodes necessary to store the information under the Guest node. All of these child nodes are XmlElement objects that are obtained by the createelement () method of the XmlDocument object. The XmlDocument object also uses the createTextNode () method to get the text information and then add it to the corresponding node later. After a reasonable addition of the guest node and its child nodes and text information, the XmlDocument object saves the information entered by the user to an XML file through the Save () method. Finally, the browser is directed to the page that displays all user input information. This way, the Web page works as shown in Figure 2:


Figure 2

Let's create a page-viewguestbook.aspx to display all user input information. In this web page, we're going to apply XSLT technology, which shows the data in the XML file we created earlier as HTML. Because the XSLT technology is used to display user input information, we do not need to add any Web controls when designing the Web page, as long as the load () method of the Web page is overloaded.

private void Page_Load (object sender, System.EventArgs e)
{
Create an XmlDocument object to load an XML file that stores information
XmlDocument xdoc = new XmlDocument ();
Xdoc. Load (Server.MapPath ("Guestbook.xml"));

Create a XslTransform object and import the XSL file
XslTransform XSLT = new XslTransform ();
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);

Show output results
Response.Write (Sr. ReadToEnd ());
}

The method first creates a XmlDocument object to load the XML data file created earlier, then creates a XslTransform object and imports the corresponding XSL file. The contents of the XSL file enable it to format the data in the original XML file as HTML and display it in the browser. Because the XSLT transformation is used, we also have to add a using SYSTEM.XML.XSL statement at the beginning of the code file.

The following is the source code for the XSL file, where the most important part is the <xsl:template match= "name" >......</xsl:template> piece.

<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= ">"
<tr>
&LT;TD valign= "Middle" align= "center" bgcolor= "Teal" colspan= "2" width= "505" height= ">"
<font style= "color:white;background-color:teal;font-family: Chinese Xingkai; font-size:x-large;font-weight:bold;" > Welcome to visit the guest book "Make Fox Rush"! </font>
</td>
</tr>
&LT;TR&GT;&LT;TD width= "505" height= "align=" left "colspan=" 2 "></td></tr>
<xsl:for-each select= "//guest" >
<xsl:apply-templates select= "Name"/>
</xsl:for-each>
<tr>
&LT;TD valign= "Middle" align= "center" colspan= "2" width= "505" >
<font>
This guest book by <a href= "mailto:0024108@fudan.edu.cn" > Wang Kaiming </a> Development! </font>
</td>
</tr>
</table>
</xsl:template>
<xsl:template match= "Name" >
<tr>
&LT;TD width= "height=" align= "right" >
<font> User name:</font>
</td>
&LT;TD width= "height=" valign= "Middle" align= "left" >
<font><xsl:value-of select= '. ' /></font>
</td>
</tr>
<tr>
&LT;TD width= "height=" align= "right" bgcolor= "e0e0e0" >
<font>email Address:</font>
</td>
&LT;TD width= "height=" valign= "Middle" align= "left" bgcolor= "#e0e0e0" >
<font><a href= "mailto:{. /email} "><xsl:apply-templates select=". /email "/></a></font>
</td>
</tr>
<tr>
&LT;TD width= "height=" align= "right" >
&LT;FONT&GT;QQ number:</font>
</td>
&LT;TD width= "height=" valign= "Middle" align= "left" >
<font><xsl:apply-templates select= ". /qq "/></font>
</td>
</tr>
<tr>
&LT;TD width= "height=" align= "right" bgcolor= "#e0e0e0" >
<font> Personal Homepage:</font>
</td>
&LT;TD width= "height=" valign= "Middle" align= "left" bgcolor= "#e0e0e0" >
<font><a href= "http://{. /homepage} "target=" _blank "><xsl:apply-templates select=". /homepage "/></a></font>
</td>
</tr>
<tr>
&LT;TD width= "height=" valign= "Top" align= "right" >
<font> Message Information:</font>
</td>
&LT;TD width= "height=" valign= "Top" align= "left" >
<font><xsl:apply-templates select= ". /comment "/></font>
</td>
</tr>
&LT;TR&GT;&LT;TD width= "505" height= "align=" left "colspan=" 2 "></td></tr>
</xsl:template>
</xsl:stylesheet>

In this way, when the user clicks on the "View Guest Book" button or the successful input information, the browser will be directed to the Web page that displays all user input information, and its effect is illustrated as follows:


Figure 3

Three Summarize:

In this way, a guest book with a basic function is complete, from which we can realize that it is fairly easy to develop Web applications using ASP.net technology, and that asp.net can become more powerful after combining XML technology. Also, the XSLT technology described in this article is very useful, and you can refer to other relevant information 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.