Asp.net + Xml implementation without database forums
**************************************** ********************
Original: fengyunwu
Date: 2003-12-6 7:37:09
Source: http://www.lshdic.com/bbs
Download: http://www.lshdic.com/download/netxmlbbs.rar
Disclaimer: the author's level is limited. If you have any omissions, typos, or mistakes, Please retain the information in the asterisk.
**************************************** ********************
Drafting: popular forum (we will focus on ASP and ASP. NET Forum) development ideas
A veteran can skip this section and read the next section.
At present, there are countless versions of various types of online forums. In the final analysis, how to ACCESS its core data warehouse is a "Database". I mean the commonly used ACCESS SQLSERVER and so on ", php asp. Net jsp, etc. These background languages use the central "ADO, SQL" line, A small part like a dashboard (Member registration, member login, forum topic category, Project Statistics, single topic, post browsing, background management ......) The building blocks are accumulated into Forum systems of different forms, and the block's "Building Blocks", however, ultimately, are the access to the database. The component is nothing more than the material that makes up wood and will not become iron, it can be seen that a good forum program is not difficult to write. It is rare to have a clear idea and a high-quality "database structure". In this case, I will start to develop ASP. NET + XML: some experiences and ideas summarized by the Forum are presented one by one without a database. If you are interested, try it yourself and try to develop the forum in a new way.
Section 1: Do not talk nonsense and answer questions
1: what knowledge should I be familiar with in this article?
Anyone who has at least one year of comprehensive WEB Background website programming knowledge can understand and be familiar with ASP. NET Programming and XML document structure programmers are more interested in
2: Can Xml act as a "Database?
Yes, but different concepts and organizational forms
3: Is it more difficult to develop databases using Xml as a forum?
It is much higher, and even some functions are limited in my own way (XML acts as a database and has some limitations compared to ACCESS and so on), but it does not mean that it is impossible
4: Why ASP is used in this article. NET is used as the background program language to combine XML to build a forum?
It has no special significance. ASP, PHP, JSP, or other background programming languages are acceptable. Even VB, VC, JAVA, and so on can express the Forum in the form of software.
5: What are the benefits of using Xml as a "Database" Forum or other systems?
For example, the program and XML structure are exquisite and complete, regardless of the size of the Program System
-Speed (file-based, relatively more worried than ADO database access)
-Resource usage (no matter the total amount of data, it is basically used to process distributed small files (usually within 1-KB). Whether it is used in memory or CPU usage, it worries about the database)
-Distributed (a single file is a collection of folders and a database that combines datasets has its own advantages)
-Portability and universality (XML itself is a common data description language, which can be smoothly transplanted and applied on different operating systems or programming languages)
-Modification, search, maintenance, and batch processing (for example, if there is a large amount of data, operations must be performed using the program system. For example, it is very difficult to manage files directly in folders, which is inferior to the interface and ease of use of the database system, unless you build your own XML data processing software or WEB system to make up for this deficiency)
-Security (big intelligence depends on the program and encryption. Little intelligence is to change the name and storage location of background files and folders, but the database structure built based on XML files is relatively scattered, not as simple as data table encapsulation)
Section 2: getting started and setting up a base (XML database structure)
Let's clarify what kind of forum system we can get started with. Here we assume that a simple forum is developed, and its functions all follow common forums. We only need one topic.
XML database implementation {
Member Data Storage (create a new file named user. xml in the main directory. The user. xml file is used as a file to store all member data. The structure of this example is as follows )[
<? Xml version = "1.0" encoding = "gb2312"?>
<Alluser>
<Fyw name = "Yunwu" pass = "1234" sex = "men" age = "20" img = "http://www.lshdic.com/bbs/image/user40.jpg" fatie = "20" exp = "243" homepage = "http://www.lshdic.com" email = "lshdic@sina.com" oicq = "21152530" qianming = "signature document content" address = "Shandong Linyi"/>
<Cike name = "Lonely Assassin" pass = "123" sex = "men" age = "20" img = "man2.gif" fatie = "0" exp = "20" homepage = "" email = "gdcike@163.com" oicq = "" qianming = "he is a pig! Ye is in the customs zone! "Address =" Linyi, Shandong Province "/>
<Plgirl name = "" pass = "123" sex = "" age = "20" img = "woman1.gif" fatie = "0" exp = "0" homepage =" "email =" "oicq =" "qianming =" "address =" "/>
</Alluser>
]
This is the XML storage structure used in this example. It is not difficult to see that a tag contains all the information of a member, and the tag name is "User Account ", name = nickname, pass = password, sex = gender ......, The "User Account" must be unique and must contain only English letters or letters with digits, XML file structures and data must comply with XML document standards and naming rules. If the data submitted by the user in the program is not processed, it may cause data leakage or the user. xml is declared as decommissioned. You must manually find and correct the xml file; otherwise, the file cannot be accessed normally.
Forum post data storage (create a folder named data in the main directory, which will save the posts posted by all members in the future, the Post File names are random or regular, such as 1.xml, 2.xml, and 3.xml. these files store all the data of the post, including the topic and reply, of course, the establishment, modification, naming, and deletion of these files will all be implemented by a program in the future. As for the program part of the new file name, ensure that its name does not conflict with any file, file names can be irregular. After all, we will not sort them by file names in the future. The content structure of a single XML file is as follows )[
1. xml:
<? Xml version = "1.0" encoding = "gb2312"?>
<? Xml-stylesheet type = 'text/xsl 'href =' ../file. aspx? Dex = 1. xml'?>
<Document>
<Record>
<Anthor> fyw </anthor>
<Title> post title </title>
<Date> 3:27:18 </date>
<Gengxindate> 3:27:18 </gengxindate>
<Body> post content
& Lt; hr & gt; The content is legal content after filtering characters whose HTML is equal to the conflicting XML document specifications, and the title also needs to be filtered
</Body>
</Record>
</Document>
2. xml:
<? Xml version = "1.0" encoding = "gb2312"?>
<? Xml-stylesheet type = 'text/xsl 'href =' ../file. aspx? Dex = 7908604. xml'?>
<Document>
<Record>
<Anthor> fyw </anthor>
<Title> post title </title>
<Date> 3:54:59 </date>
<Gengxindate> 3:54:59 </gengxindate>
<Body> post content </body>
<Reply>
<Anthor> cike </anthor>
<Date> 5:53:38 </date>
<Gengxindate> 5:53:38 </gengxindate>
<Body> content replied on the first floor </body>
</Reply>
<Reply>
<Anthor> plgirl </anthor>
<Date> 5:53:38 </date>
<Gengxindate> 5:53:38 </gengxindate>
<Body> content replied on the second floor </body>
</Reply>
</Record>
</Document>
]
This is the XML storage structure of the forum post in this example. Now we have created a member data storage file (user. xml) and the location and structure of Forum posts (data \*. xml), are you inspired? Is it true that a program can build a complex forum system on these two cornerstones? The answer is yes. As long as the two storage bases can be manipulated in the background through the program and the user-friendly interface, you can try it out.
}
Section 3: Zhuge columns and soldiers
To complete the XML data storage structure, the following lists the materials required to complete the Forum's great business.
Project includes {
General function page
(Create a common function page, DLL, or user control such as "1" or "2", and place common program processes, variables, and functions in it. This can greatly save time and avoid repeated work, this will be mentioned in the next chapter)
Differentiation between visitors and members
(That is, a session. In this example, it is the judgment of whether the session ("who") value exists. If it exists, it is the member. If it does not exist, it is the visitor, in this example, we do not use cookies to store user data. Naturally, visitors can only browse posts, not post or reply to posts, but Members can)
Member registration
(The page that contains various forms is used to complete the request to the user. add a new label node in xml (equivalent to adding a new member data), and process the data submitted by the user, the method must be used to prevent cross-site submission and member account (that is, XML node header label) the regular expression must be used to limit the syntax requirements to similar program variables, or simply only English letters and XML node attributes must be allowed to at least filter <> & "'and press ENTER characters to prevent applying member accounts from using user exists. xml, all data English symbols are converted to lowercase format, that is, case-insensitive, including user accounts and passwords)
Member login
(Including the user account input box, password input box, and a login button. After submission, use XMLDOM to search for the user. whether the xml has a matched user Account (especially filter the data submitted by the user, especially the * number, which is considered a wildcard in the XMLDOM search), such as user. xml contains this account, that is, a session. In this example, the session ("who") is set to the user account, and the visitor's identity becomes a member identity)
Member Information Modification
(Visitors are prohibited from browsing this page. The style remains basically the same as the form on the member registration page (but does not include the function of modifying the user account name), except that the values of all forms are pre-read users. match session ("who") information of this Member in xml. After the user modifies the information, click the modify button. Then, the background program modifies the user. the node data corresponding to this member in xml)
Member Information Display
(Read-only page, read the corresponding member account in the URL parameters, show all member information without confidentiality)
Homepage of a forum topic
(That is, the post List displays all the post information of the corresponding page number in the data directory by Page Based on the URL page number parameter. The sorting is based on the file modification date, and the latest update is the top-notch, this is one of the most difficult projects to develop in this forum. We should be careful when it is necessary to Extract files from the data directory by page in the form of point-to-point (I .e. 1-10, 30-40,100-110, can ensure the best running speed)
Post display page
(The post in this example shows that the XML file is accessed directly in the browser, that is, the XSL file specified by the href of the XML-stylesheet node after opening the xml file --- 2: because the target file type is aspx, we access the corresponding XML file data in the target aspx according to the url parameter, and control the output format of the data stream as xsl, the ideal xsl style table is well organized in the Cong, which plays a key role in the middle layer --- 3: Because the xsl output by aspx is organized in the background, it is highly intelligent and reasonable, that is, the post display page with complicated XML data output structure can be used. During this period, the development of the middle layer is the most difficult and the most difficult step in this forum system, in simple terms, XMLDOM analyzes and extracts node data from the target XML to display the topic, post author, post content, and all replies to the post. However, the most difficult part is that when I started development, it is impossible to resolve the pagination of post replies, as well as conflicts between aspx server-side controls and xsl document specifications, which may be due to my limited level or the inherent defects of my original three-layer output, XML is easy to implement databases, but it is difficult to achieve ideal output beyond HTML through XSL)
Post New posts
(Prohibit visitors from posting. This project is an input box + content input box + submit button for the input topic. You can bind the project to the bottom of the column post list page or create a special page. after submitting the project, you must use the server. HTMLEncode () filters data submitted by the user, then, use the program to create an XML file, name the XML file, organize the XML file format, such as the format written in section 2, write filtered data to the node, and modify the user. the nodes of the posting member corresponding to xml realize the points + 2 and the number of posts + 1, and use cookies to prohibit multiple submission for irrigation within 7 seconds)
Reply to new post
(Visitors are prohibited from replying. This page can be bound to the lower part of the Post display page or a new major. A content input box + submit button is provided. Data filtering is required if a new post is published, insert a reply node in the corresponding post storage XML file, create a node in the node for storing the reply author name, reply content, reply date, reply update date, and so on, and modify the user. the nodes of the posting member corresponding to xml realize the points + 1 and the number of posts + 1, and use cookies to prohibit multiple submission for irrigation within 7 seconds)
Moderator Management
(To implement password verification and determine whether visitors and members are prohibited from using the password, the optional backend management functions include bonus Member points, reduction of Member points, deletion of members, modification of any posts, and deletion of any posts, you can even consider many advanced features, such as IP address blocking, pin, lock, and moderator)
}
Section 4: junlin sandground and tips
After the three sections above, if you study carefully, you must be confident and eager to try it. At this time, you must have started, or you may have a clear idea but do not know how to write the program. If so, no need to worry, I will first list some of my life's commonly used "War of war" one by one, it is set to make you feel better
1: Error prompt function (used when needed. A dialog box is displayed to remind users of the error, and then the previous page is automatically returned. This function is recommended to be saved as a common file. <! -- # Include file = "publicfun. aspx" --> click it to use it)
<Script language = vb runat = 'server'>
Sub t2 (tstr2 as string)
Response. write (replace ("<script language = vbs> msgbox" & chr (34) & tstr2 & chr (34) & ", 16," & chr (34) & "error message" & chr (34) & ": history. back () & lt;/script> "," & lt; "," <"): response. end
End sub
</Script>
2: Common Data Detection Functions (recommended for modifying XML tag attributes for general detection. User Accounts also require more rigorous data filtering)
<Script language = vb runat = 'server'>
Sub jiancha (requeststr as string) 'filtering in this example '"<> & *. If any omission exists, add
Dim array1 (5) as string
Array1 (0) = "'": array1 (1) = chr (34): array1 (2) = "<": array1 (3) = "> ": array1 (4) = "&": array1 (5) = "*"
Dim tempi as integer
For tempi = 0 to ubound (array1)
If instr (requeststr, array1 (tempi) <> 0 then response. write (replace ("<script language = vbs> msgbox" & chr (34) & "Reference Data" & requeststr & "cannot contain the prohibition symbol [" & array1 (tempi) & "]. Correct the automatic return value" & chr (34) & ", 16," & chr (34) & "error message" & chr (34) & vbcrlf & "history. back () & lt;/script> "," & lt; "," <"): response. end
Next
End sub
</Script>
3: asp.net rejects cross-site submission injection (it can be used on many pages such as Member registration and member modification pages)
<%
If lcase (mid (request. serverVariables ("HTTP_REFERER"), 8, len (request. serverVariables ("SERVER_NAME") <> lcase (request. serverVariables ("SERVER_NAME") then t2 ("cross-site submission is denied! ")
%>
4: Add a new node to the target XML file
<% @ Import namespace = 'System. xml' %>
<%
Dim userdom1 = new xmldocument, userdom2, userdom3
Userdom1.load (server. mappath ("user. xml") 'loads the XML file to be operated
Userdom2 = userdom1.selectsinglenode ("alluser") 'operation cursor pointing to alluser node, handle to userdom2 variable
Userdom3 = userdom1.createElement ("fyw") 'creates a node named fyw and returns the handle to the userdom3 variable.
Userdom3.setAttribute ("name", "Yunwu") 'adds a node attribute for userdom3. The new attribute name is name and the value is Yunwu.
Userdom3.setAttribute ("pass", "123 ")
Userdom3.innerText = "test" 'sets the data contained by the userdom3 Node
Userdom2.AppendChild (userdom3) 'userdom2 is the fyw node described by userdom3 added to the alluser node.
Userdom1.save (server. mappath ("user. xml") 'saves the changed XML data to user. xml
%>
5. Modify an XML Node
<% @ Import namespace = 'System. xml' %>
<%
Dim userdom1 = new xmldocument, userdom2
Userdom1.load (server. mappath ("user. xml "))
Userdom2 = userdom1.selectsinglenode ("alluser"). getElementsBytagname ("fyw") 'operation cursor points to the fyw node under the alluser Node
Userdom2 (0). setAttribute ("name", "Yunwu") 'sets the first fyw node array and changes its name attribute value to Yunwu.
Userdom2 (0). innerText = "test" 'sets the first array of fyw nodes and changes the data contained in it to test
Userdom1.save (server. mappath ("user. xml") 'saves the changed XML data to user. xml
%>
6: search to determine whether an XML node exists
<% @ Import namespace = 'System. xml' %>
<%
Dim userdom1 = new xmldocument
Userdom1.load (server. mappath ("user. xml") 'loads the XML file to be operated
'The following is exactly why I want to use the "User Account" to identify the unique identity of a user. It is convenient to find it :)
If userdom1.selectsinglenode ("alluser"). getElementsBytagname ("fyw"). count <> 0 then t2 ("fyw node already exists ")
%>
7: Add XML nodes in batches (it is very difficult to add nodes one by one using the method mentioned in the 4th example when writing a reply post. In this case, you can use this method to add nodes in batches)
<% @ Import namespace = 'System. xml' %>
<%
Dim filedom = new xmldocument, filedom2
Filedom. load (server. mappath ("data \" & request. querystring ("dex") 'request. querystring ("dex") = 6.xml
Filedom2 = filedom. createElement ("reply ")
'The following method is very convenient, and the structure and layout of the XML file are also good. Reasonable spaces and carriage returns can provide a good XML document structure.
Filedom2.innerXml = vbcrlf & "<anthor>" & session ("who ") & "</anthor>" & vbcrlf & "<date>" & now & "</date>" & vbcrlf & "<gengxindate>" & now & "</gengxindate> "& vbcrlf &" <body> "& neirong. value & "</body>" & vbcrlf &""
Filedom. selectsinglenode ("document/record"). appendChild (filedom2)
Filedom. save (server. mappath ("data \" & request. querystring ("dex") 'request. querystring ("dex") = 6.xml
%>
8. delete an XML node.
<% @ Import namespace = 'System. xml' %>
<%
Dim userdom1 = new xmldocument, userdom2
Userdom1.load (server. mappath ("user. xml "))
Userdom1.DocumentElement. RemoveChild (userdom1.selectsinglenode ("alluser/fyw") 'deletes all fyw nodes under the alluser Node
Userdom1.save (server. mappath ("user. xml "))
%>
9: Create a New XML file (any type of suffix can be created, not for XML files)
<% @ Import namespace = 'System. io '%>
<%
Dim newfile = new StreamWriter (server. mappath ("data \" & new DirectoryInfo (server. mappath ("data ")). getfiles (). length + 1 &". xml "), false, System. text. encoding. default)
'The above we specify the default encoding method as System. text. encoding. default, or you can change it to System. text. encoding. getEncoding ("gb2312"), otherwise it will not work properly if the XML contains Chinese characters as UTF-8 encoding
Newfile. write ("<? Xml version = "& chr (34) &" 1.0 "& chr (34) &" encoding = "& chr (34) &" gb2312 "& chr (34) & "?> "&_
Vbcrlf & "<? Xml-stylesheet type = 'text/xsl 'href =' ../file. aspx? Dex = "& filelength + 1 &". xml'?> "& Vbcrlf &_
"<Document>" & vbcrlf & "<record>" & vbcrlf & "<anthor>" & session ("who") & "</anthor>" & vbcrlf &_
"<Title>" & server. HTMLEncode (biaoti. value) & "</title>" & vbcrlf & "<date>" & now & "</date>" & vbcrlf &_
"<Gengxindate>" & now & "</gengxindate>" & vbcrlf & "<body>" & server. HTMLEncode (neirong. text) & "</body>" & vbcrlf &_
"</Record>" & vbcrlf & "</document> ")
Newfile. close: newfile = nothing
%>
10: delete a specified file
<% @ Import namespace = 'System. io '%>
<% File. delete (server. mappath ("data \ 2.xml") %>
11: use cookies to prevent repeated bumping within 7 seconds
<%
If not request. cookies ("lshdicbbs") is nothing then
If isdate (request. cookies ("lshdicbbs") ("guanshui") = false then response. cookies ("lshdicbbs") ("guanshui") = now
If datediff ("s", request. cookies ("lshdicbbs") ("guanshui"), now) <7 then response. write ("prohibit repeated post bumping within 7 seconds"): response. cookies ("lshdicbbs") ("guanshui") = now: response. end
End if
Response. cookies ("lshdicbbs") ("guanshui") = now
%>
12: use regular expressions to restrict the data submitted by users to English letters (if you are interested, you can extend it to support letters and numbers)
<%
Dim name1 = request. form ("username ")
If regex. replace (name1, "[a-z] +", "", RegexOptions. ignoreCase) <> "" then t2 ("the account name must use an English letter in the-Za-z range ")
%>
13: Extended prompt tool (the original script can be modified as needed)
<Div style = 'position: absolute; left: 0; top: 0; border-bottom: 1 solid green; border-right: 1 solid green; border-left: 1 solid cccccc; border-top: 1 solid cccccc; display: none; z-index: 500; background-color: # FFF7FF; padding: 2; white-Space: nowrap; table-Layout: fixed; 'id = showdiv> </div>
<Script>
Var oldtext = "Acceleration variable", colors1 = new Array ("# FFECD5", "# FFF7FF", "# FFFFEB", "white", "# F5FFEB ", "# EEFAFF", "# FFFFEE", "# EDFFFC ")
Function document. onmousemove (){
Try {
If (event. srcElement. getAttribute ('lshdic '))
{
Showdiv. style. left = event. X-3; showdiv. style. top = event. y + document. body. scrollTop + 18; if (event. srcElement. lshdic! = Oldtext) {oldtext = event. srcElement. lshdic; showdiv. innerText = oldtext; showdiv. style. backgroundColor = colors1 [Math. round (Math. random () * colors1.length)]}; if (showdiv. style. display = 'None') showdiv. style. display =''
} Else {if (showdiv. style. display = '') showdiv. style. display = 'none' ;}} catch (e ){}
}
</Script>
<A href = "http://www.lshdic.com" lshdic = 'Welcome to the author's website'> lanli.com </a>
<A href = "http://www.lshdic.com/bbs" lshdic = 'Welcome to Lili Technology Forum & #13; & #13; 10:31:32 '> Lili Technology Forum </a>
Section 5: Give me a friend here
After the hard work in the above four chapters, I believe that you have gained a basic understanding of how to use background programs in combination with XML databases to implement complex forums and systems, but reading N-sides does not actually work, you must do it first to sum up your experience in practice. This article is just a reference.
Due to limited time, I can only develop ASP by myself. NET version of the XML database-free forum (full name: Lili NetXml database-free forum 1.0) for your research, due to my ASP. NET Virtual space is a bit of a problem, cannot provide online demonstration, now only provide download of all the original code (the above 13 original code is mostly picked in the system), address: http://www.lshdic.com/download/netxmlbbs.rar, download the reference help file, in your asp. . NET Server.