ASP.NET + XML to create a message

xiaoxiao2021-03-06  135

One. Overview: The message book is an important part of the website. It is the place where visitors commented on the website, and the website administrator understands the powerful tools for the basic operation of the website, so the message board plays a very important role in the current website.

However, it is not an easy thing to develop a message book before, and the developer's work will tend to be large. Now, with Microsoft launched VS.NET, the corresponding technology also pushes new. In particular, XML technology is widely used in .NET Framework, the entire .NET architecture has a very superior foundation. The new programming model launched in ASP.NET makes it easy to develop web applications. This article combines the advantages of ASP.NET technology and XML technology to introduce you how to create a message book.

two. 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@tang.com 10102350 < HomePage> www.doose.com This message 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 it carefully "to use ASP. NET and XML technology to create a message book "

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.ext {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");}

Among them, the first button is the most important, it can store the user's input information into the XML file, the method called the way is saveXmldata (); and the second button only complements the copy of the text box; third The function of the button is 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 that loads 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 nodes XMLELEMENT NAMENODE = xdoc for storage information. 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); // Adds the nodes of each stored information created above to the guest node but does not include the final value of ParentNode.Appendchild (NameNode); ParentNode.Appendchild (emailnode); ParentNode.Appendchild (Qqnode); ParentNode.Append Child (homepageNode); parentNode.AppendChild (commentNode); // add to obtain the above text information corresponding thereto node nameNode.AppendChild (nameText); emailNode.AppendChild (emailText); qqNode.AppendChild (qqText); HomepageText; CommentNode.Appendchild (CommentText); // Save XML file XDoc.save for 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, these classes are included in the system.xml namespace, so please follow the code file. Add the statement of use system.xml at the beginning.

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 run is 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 files 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 results Response.write (sR.ReadToeend ());} This method first creates an XMLDocument object to load the XML data files created in front, then create an XSLTransform object and import the appropriate 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 ... .

This message book is developed by Wang Kaiming
User Name:
email address:
QQ number: < Tr> Personal home page:
转载请注明原文地址:https://www.9cbs.com/read-127070.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.064, SQL: 9