ASP.NET + XML to create a message

xiaoxiao2021-03-06  104

Implementation:

A basic message board should include at least two functions: accept the information input by the user and save the information to the background database; display the information input by the user. The information entered by the user generally includes user name, email address, QQ number, user home page, message information, etc., which is usually saved in a table of the background database, but this article should be used to store this information. When the information entered by the user is generally displayed, the method here is to read the data from the XML file and use the XSLT technology to format it, and finally appear in the browser in the form of HTML.

In this way, our message book requires two web pages, one for accepting user input information, and the other is used to display the information that the user has entered. The XML file (guestbook.xml) of storage information is required:

Linghu Chong

Doose@etang.com

10102350

Www.doose.com

This guest book is created by "Linghu Chong", I hope you can like it :) To know how to create a message belonging to your own message, please read "Use ASP.NET and XML technology to create a message"!

Let's first create a web page for accepting user input information - Homebook.aspx. According to the basic requirements mentioned earlier, the web page includes the following sections: a message book title, "User Name:" Tag and Input Box, "Email Address:" Tags and Input Boxes, "QQ Number:" Tags and Enter Box, "Personal Home:" Tag and Enter Box, "Message Information:" Tags and Enter Boxes, a "OK" button, a "reset" button, a "View Message Book" button, and the page also includes two A verification button is used to verify that the username and whether the email address is empty. If it is empty, it will be entered. At the same time, in order to make the message book have a good user interface, I use the form to make a page arrangement, so that all components in the message book can have a reason, and the level is clear. For details on the web page, please refer to the source code included after the text, this is not given here. The image layout is shown below:

figure 1

Complete the arrangement of the web page, we only completed a part of the work, and we didn't have a real encoding. I think everyone is definitely understanding or familiar with the code post technology in the ASP.NET. It is divided into the layout work of the web page and the rear-ended encoding work area to achieve a good separation effect. Below we write a message corresponding to the three buttons in 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) {// Show all users' message information response.redirect ("ViewGuestbook.aspx");} where the first button is most important, it can input users Information stored in an XML file, the method called the way is saveXmldata (); and the second button only completes the reset empty work of the text box; the third button is used to display all user input information using another web page. At the same time, the first button will also guide the browser to the page displaying all users enter information after successful storage information. Let's take a detailed analysis of the SaveXmldata () method, which is achieved as follows:

Private void saveXmldata () {Try {// Create an XMLDocument object to load the XML file XMLDocument xdoc = new xmldocument (); xdoc.low ("guestbook.xml");

// Create a new guest node and add it to the root node XMLELEMENTNODE = XDoc.createElement ("Guest"); xdoc.documentelement.prependchild (ParentNode);

// create all the nodes XmlElement nameNode for storing information = xdoc.CreateElement ( "name"); XmlElement emailNode = xdoc.CreateElement ( "email"); XmlElement qqNode = xdoc.CreateElement ( "qq"); XmlElement homepageNode = xdoc .CreateElement ("HomePage"); Xmlelement CommentNode = xdoc.createElement ("Comment");

// Get the 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 nodes each store information created above to the guest node but does not include a final value parentNode.AppendChild (nameNode); parentNode.AppendChild (emailNode); parentNode.AppendChild (qqNode); parentNode.AppendChild (homepageNode) ParentNode.Appendchild (CommentNode);

// Add the above acquired text information corresponding thereto to the node nameNode.AppendChild (nameText); emailNode.AppendChild (emailText); qqNode.AppendChild (qqText); homepageNode.AppendChild (homepageText); commentNode.AppendChild (commentText ); // Save the XML file xdoc.save of the store information (Server.MAppath ("Guestbook.xml");

/ / Show all users' message information response.redirect ("ViewGuestbook.aspx");} catch (exception e) {}}}

This method mainly uses the XMLDocument class, XMLELEMENT, and XMLText classes, which are included in the System.xml namespace, so add the USING SYSTEM.XML statement at the beginning of the code file. This method uses a Try-Catch statement block that first loads an XML file by creating an XMLDocument object, then creating a son-Home node of the root node and adding the five child nodes necessary for the storage information under the Guest node. All of these child nodes are XMLELEMENT objects, which are obtained by 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. After reasonable adding guest nodes and their subtots and text messages, the XMLDocument object saves the information entered into the XML file via the save () method. Finally, the browser will guide the page that displays all users entered information. In this way, the effect of the web page is as shown in Figure 2:

figure 2

Let's create a page for displaying all users -ViewGuestbook.aspx. In this web page, we should use XSLT technology, which can display the data in the previous XML file in HTML. Since it is used to display the user input information using XSLT technology, we do not need to add any web controls when designing the web page, just overload the LOAD () method of the web page.

Private void page_load (object sender, system.eventargs e) {// Create an XMLDocument object to load storage information XMLDocument xdoc = new xmldocument (); xdoc.low (Server.MAppath ("Guestbook.xml")) ;

// Create an XSLTransform object and import 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);

// Display output Response.write (Sr.ReadToeend ());} This method first creates an XMLDocument object to load the XML data files previously created, then create an XSLTransform object and import the corresponding XSL file. Through the content in this XSL file, it can format data in the original XML file into the form of HTML and displayed in the browser. Because of the use of XSLT conversion, we have to add the statement of the Using System.xml.xsl at the beginning of the code file.

The following is the source code of the XSL file, where the most important part is

......

piece.

http://www.w3.org/1999/xsl/transform "Version =" 1.0 ">

Welcome to the message book of "Linghu Chong"! This guest book is developed by mailto: 0024108@fudan.edu.cn> Wang Kaiming!

username:

Email address:

Mailto: {../ email}>

SELECT = "../ email" /> QQ number:

Personal Homepage: http: // {../homepage} "target =" _ blank ">

voicemail:

This way, when the user clicks the "View Message Book" button or successfully entered the message, the browser is guided to the web page of all users entering information, and its running effect is shown below: <>

转载请注明原文地址:https://www.9cbs.com/read-125933.html

New Post(0)