最近没事做!整了一个简单的XML的留言簿!也算多学点东西吧!自己总结一下!也把经验给跟像我一样的人分享一下!废话少讲!把代码帖出来吧!整个网页功能很简单,只有发帖跟回复两个功能,当然具然具有管理功能的ID可以删除回复跟帖子!考虑到我做的只是一个公司内部用!我只做了三个XML文件,用户文件user.xml,主题文件bbs.xml,回复文件bbsre.xml!下面把结购帖下:user.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<bbsuser>
<user>
<name>陈胜军</name>
<password>chuxue</password>
<group>2</group>
<date>2007-07-02</date>
<qq>113620916</qq>
<email>chuxue1342@hotmail.com</email>
<telephone>13450051690</telephone>
<header>images/5.gif</header>
</user>
</bbsuser>
BBS.XML
<?xml version="1.0" encoding="utf-8"?>
<bbs>
<bbsid>
<id>1</id>
<title>SWEDA内部论坛</title>
<author>陈胜军</author>
<content>可以显示就OK了!</content>
<date>2007-07-02</date>
<ip>192.9.1.66</ip>
</bbsid>
</bbs>
BBSRE.XML
<?xml version="1.0" encoding="utf-8"?>
<bbsre>
<re>
<id>1</id>
<reid>1</reid>
<author>陈胜军</author>
<date>2007-07-02</date>
<content>我回复sweda内部论坛了!!!!</content>
<ip>192.9.1.66</ip>
</re>
</bbsre>
后台用了两层:
data.vb
Imports system.data
Imports system.data.sqlclient
Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.IO
Public Class data
Inherits System.Web.UI.Page
''' <summary>
''' 根据ID,XML文件返回表
''' </summary>
''' <param name="id"></param>
''' <param name="path"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function getdata(ByVal id As Integer, ByVal path As String) As DataView
Dim dt As DataTable = New DataTable()
Dim dtr As DataTable = New DataTable()
dtr.Columns.Add("id", GetType(String))
dtr.Columns.Add("content", GetType(String))
dtr.Columns.Add("author", GetType(String))
dtr.Columns.Add("date", GetType(String))
dtr.Columns.Add("ip", GetType(String))
dtr.Columns.Add("reid", GetType(String))
dtr.Columns.Add("new", GetType(String))
Dim dv As DataView = New DataView()
Dim stream As FileStream = New FileStream(Server.MapPath(path), FileMode.Open)
Dim doc As XmlDataDocument = New XmlDataDocument
doc.DataSet.ReadXml(New StreamReader(stream))
dt = doc.DataSet.Tables(0)
Dim dr() As DataRow = dt.Select("id=" & id)
Dim i As Integer
For i = 0 To dr.Length - 1
dtr.Rows.Add(New Object() {dr(i).Item("id").ToString, dr(i).Item("content").ToString, dr(i).Item("author").ToString, dr(i).Item("date").ToString, dr(i).Item("ip").ToString, dr(i).Item("reid").ToString})
Next
dv = dtr.DefaultView
stream.Close()
Return dv
End Function
''' <summary>
''' 根据ID,XML文件返回BBS主题
''' </summary>
''' <param name="id"></param>
''' <param name="path"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function getbbs(ByVal id As Integer, ByVal path As String) As DataView
Dim dt As DataTable = New DataTable()
Dim dtr As DataTable = New DataTable()
dtr.Columns.Add("id", GetType(String))
dtr.Columns.Add("content", GetType(String))
dtr.Columns.Add("author", GetType(String))
dtr.Columns.Add("date", GetType(String))
dtr.Columns.Add("title", GetType(String))
dtr.Columns.Add("new", GetType(String))
dtr.Columns.Add("ip", GetType(String))
Dim dv As DataView = New DataView()
Dim stream As FileStream = New FileStream(Server.MapPath(path), FileMode.Open)
Dim doc As XmlDataDocument = New XmlDataDocument
doc.DataSet.ReadXml(New StreamReader(stream))
dt = doc.DataSet.Tables(0)
Dim dr() As DataRow = dt.Select("id=" & id)
Dim i As Integer
For i = 0 To dr.Length - 1
dtr.Rows.Add(New Object() {dr(i).Item("id").ToString, dr(i).Item("content").ToString, dr(i).Item("author").ToString, dr(i).Item("date").ToString, dr(i).Item("title").ToString, dr(i).Item("date").ToString, dr(i).Item("ip").ToString})
Next
dv = dtr.DefaultView
stream.Close()
Return dv
End Function
''' <summary>
''' 根据用户名返回头像
''' </summary>
''' <param name="author"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function getimage(ByVal author As String) As String
Dim path As String = ""
Dim Doc As New XmlDocument
Doc.Load(Server.MapPath("user.xml"))
Try
Dim xnl As XmlNodeList = Doc.GetElementsByTagName("name")
Dim xn As XmlNode
For Each xn In xnl
If xn.InnerText = author Then
Dim xe As XmlElement
xe = xn
Dim xp As XmlElement = xe.ParentNode
path += xp.LastChild.InnerText
End If
Next
Catch ex As Exception
path += "images/10.gif"
End Try
Return path
End Function
''' <summary>
''' 获取回复ID
''' </summary>
''' <param name="pid"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function getreid(ByVal pid As String) As String
Dim a1 As New ArrayList
Dim doc As New XmlDocument
doc.Load(Server.MapPath("bbsre.xml"))
Dim xnl As XmlNodeList = doc.GetElementsByTagName("reid")
Dim xn As XmlNode
For Each xn In xnl
Dim xe As XmlElement = xn
If xe.PreviousSibling.InnerText = pid Then
a1.Add(xe.InnerText)
End If
Next
a1.Sort()
Dim reid As String
If a1.Count = 0 Then
reid = 0
Else
reid = a1.Item(a1.Count - 1).ToString
End If
Return reid + 1
End Function
''' <summary>
''' 删除回复内容
''' </summary>
''' <param name="bbsid"></param>
''' <remarks></remarks>
Sub delreply(ByVal bbsid As String)
Dim doc As New XmlDocument
doc.Load(Server.MapPath("bbsre.xml"))
Dim xnl As XmlNodeList = doc.SelectNodes("//bbsre/re/id")
Dim xn As XmlNode
For Each xn In xnl
Dim xe As XmlElement = xn
If xn.InnerText = bbsid Then
Dim xp As XmlElement = xe.ParentNode
Dim root As XmlElement = xp.ParentNode
root.RemoveChild(xp)
doc.Save(Server.MapPath("bbsre.xml"))
End If
Next
End Sub
''' <summary>
''' 根据用户名获取组ID
''' </summary>
''' <param name="user"></param>
''' <returns></returns>
''' <remarks></remarks>
Function getgroup(ByVal user As String) As String
Dim groupid As String = ""
Dim doc As New XmlDocument
doc.Load(Server.MapPath("user.xml"))
Dim xnl As XmlNodeList = doc.GetElementsByTagName("name")
Dim xn As XmlNode
For Each xn In xnl
Dim xe As XmlElement = xn
If xe.InnerText = user Then
Dim xs As XmlElement = xe.NextSibling
Dim group As XmlElement = xs.NextSibling
groupid += group.InnerText
End If
Next
Return groupid
End Function
End Class
主题后台代码:bbsindex.aspx.vb
Imports System.Xml
Imports System.IO
Imports System.Data
Imports System.Net
Imports data
Partial Class bbs
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
binddatalist()
If Session("username") = "" Then
addnewbt.Enabled = False
Else
hfuser.Value = Session("username").ToString
End If
End If
End Sub
Protected Sub enterbt_Click(ByVal sender As Object, ByVal e As System.EventArgs) '增加新帖提交按钮
Dim doc As New XmlDocument
doc.Load(Server.MapPath("bbs.xml"))
Dim author As String = hfuser.Value
Dim ip As String = Page.Request.UserHostAddress
Dim dates As String = String.Format(Now.Date(), "yyyy-MM-dd")
Dim bbsid As XmlElement = doc.CreateElement("bbsid")
Dim id As XmlNode = doc.CreateElement("id")
id.InnerText = getmaxid()
Dim tt As XmlNode = doc.CreateElement("title")
tt.InnerText = title.Text
Dim ctt As XmlNode = doc.CreateElement("content")
ctt.InnerText = content.Text
Dim user As XmlNode = doc.CreateElement("author")
user.InnerText = author
Dim dt As XmlNode = doc.CreateElement("date")
dt.InnerText = dates
Dim ips As XmlNode = doc.CreateElement("ip")
ips.InnerText = ip
bbsid.AppendChild(id)
bbsid.AppendChild(tt)
bbsid.AppendChild(ctt)
bbsid.AppendChild(user)
bbsid.AppendChild(dt)
bbsid.AppendChild(ips)
Dim root As XmlElement = doc.SelectSingleNode("bbs")
root.AppendChild(bbsid)
doc.Save(Server.MapPath("bbs.xml"))
Response.Write("<script>javascript:alert('添加成功!!!');</script>")
Context.Server.ScriptTimeout = 60 * 10
binddatalist()
Panel1.Visible = False
End Sub
Protected Sub cancelbt_Click(ByVal sender As Object, ByVal e As System.EventArgs) '增加新帖取消按钮
title.Text = ""
content.Text = ""
End Sub
Function getmaxid() As String
Dim doc As New XmlDocument
doc.Load(Server.MapPath("bbs.xml"))
Dim xnl As XmlNodeList = doc.GetElementsByTagName("id")
Dim xn As XmlNode = xnl(xnl.Count - 1)
Dim maxid As String = xn.InnerText + 1
Return maxid
End Function
Protected Sub addnewbt_click(ByVal sender As Object, ByVal e As System.EventArgs) '发表新帖,显示panel1
Panel1.Visible = True
End Sub
Sub managebt_click(ByVal sender As Object, ByVal e As DataListCommandEventArgs) Handles mydatalist.ItemCommand '删除主题,删除回复内容
Dim mysql As data = New data
Dim author As String = hfuser.Value
Dim groupid As String = mysql.getgroup(author)
If groupid = "2" Then
Dim hf As HiddenField = CType(e.Item.FindControl("hfid"), HiddenField)
Dim bbsid As String = hf.Value
Dim doc As New XmlDocument
doc.Load(Server.MapPath("bbs.xml"))
Dim xnl As XmlNodeList = doc.SelectNodes("//bbs/bbsid/id")
Dim xn As XmlNode
For Each xn In xnl
Dim xe As XmlElement = xn
If xe.InnerText = bbsid Then
mysql.delreply(bbsid)
Dim xp As XmlElement = xe.ParentNode
Dim root As XmlElement = xp.ParentNode
root.RemoveChild(xp)
doc.Save(Server.MapPath("bbs.xml"))
Response.Write("<script>javascript:alert('删除成功!!!');</script>")
Context.Server.ScriptTimeout = 60 * 10
binddatalist()
End If
Next
Else
Response.Write("<script>javascript:alert('对不起,你没权限删除本帖!!');</script>")
End If
End Sub
Sub binddatalist() '绑定主题列表datalist
Dim stream As FileStream = New FileStream(Server.MapPath("bbs.xml"), FileMode.Open)
Dim doc As XmlDataDocument = New XmlDataDocument
doc.DataSet.ReadXml(New StreamReader(stream))
If doc.DataSet.Tables.Count > 0 And doc.DataSet.Tables(0).Rows.Count > 0 Then
Dim dv As DataView = doc.DataSet.Tables(0).DefaultView
dv.Sort = "id desc"
mydatalist.DataSource = dv
mydatalist.DataBind()
End If
stream.Close()
End Sub
End Class
回复帖子页面:
Imports System.Xml
Imports System.IO
Imports System.Data
Imports data
Imports System.Net
Imports FreeTextBoxControls
Partial Class bbsview
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim mysql As data = New data
Dim intid As Integer
intid = Page.Request("id").ToString
Dim dv1 As DataView = mysql.getbbs(intid, "bbs.xml")
mydatalist.DataSource = dv1
mydatalist.DataBind() '绑定主题列表
binddatalist2()
hfip.Value = Page.Request.UserHostAddress
If Session("username") = "" Then
replybt.Enabled = False
Else
hfuser.Value = Session("username").ToString
End If
End If
End Sub
Function getreimage(ByVal author As String) As String '获取人物头像
Dim mysql As data = New data
Dim path As String = mysql.getimage(author)
Return path
End Function
Protected Sub replybt_Click(ByVal sender As Object, ByVal e As System.EventArgs) '回复按钮
Dim author As String = hfuser.Value
Dim bbsid As String = Page.Request("id").ToString
Dim ip As String = hfip.Value
Dim doc As New XmlDocument
doc.Load(Server.MapPath("bbsre.xml"))
Dim root As XmlNode = doc.SelectSingleNode("bbsre")
Dim xe As XmlElement = root
Dim re As XmlElement = doc.CreateElement("re")
Dim id As XmlNode = doc.CreateElement("id")
id.InnerText = bbsid
Dim authors As XmlNode = doc.CreateElement("author")
authors.InnerText = author
Dim dates As XmlNode = doc.CreateElement("date")
dates.InnerText = String.Format(Now.Date(), "yyyy-MM-dd")
Dim content As XmlNode = doc.CreateElement("content")
content.InnerText = FreeTextBox1.Text
Dim reip As XmlNode = doc.CreateElement("ip")
reip.InnerText = ip
Dim reid As XmlNode = doc.CreateElement("reid")
Dim mysql As data = New data
reid.InnerText = mysql.getreid(bbsid)
re.AppendChild(id)
re.AppendChild(reid)
re.AppendChild(authors)
re.AppendChild(dates)
re.AppendChild(content)
re.AppendChild(reip)
xe.AppendChild(re)
doc.Save(Server.MapPath("bbsre.xml"))
binddatalist2()
FreeTextBox1.Text = ""
End Sub
Protected Sub cancelbt_click(ByVal sender As Object, ByVal e As System.EventArgs) '重置按钮
FreeTextBox1.Text = ""
End Sub
Sub delbt_click(ByVal sender As Object, ByVal e As DataListCommandEventArgs) Handles datalist2.ItemCommand '删除回复
If Session("username") <> Page.Request("author").ToString Then
Response.Write("<script>javascript:alert('对不起,你没有删除的权限!!');</script>")
Exit Sub
Else
Dim author As String = hfuser.Value
Dim bbsid As String = Page.Request("id").ToString
Dim hf As HiddenField = CType(e.Item.FindControl("reid"), HiddenField)
Dim reid As String = hf.Value
delreply(reid, bbsid)
binddatalist2()
End If
End Sub
Sub delreply(ByVal reid As String, ByVal pid As String) '删除回复内容
Dim doc As New XmlDocument
doc.Load(Server.MapPath("bbsre.xml"))
Dim xnl As XmlNodeList = doc.SelectNodes("//bbsre/re/reid")
Dim xn As XmlNode
For Each xn In xnl
Dim xe As XmlElement
xe = xn
If xe.InnerText = reid And xe.PreviousSibling.InnerText = pid Then
Dim xp As XmlElement = xe.ParentNode
Dim xpp As XmlElement = xp.ParentNode
xpp.RemoveChild(xp)
End If
Next
doc.Save(Server.MapPath("bbsre.xml"))
End Sub
Sub binddatalist2() '绑定回复列表
Dim mysql As data = New data
Dim intid As Integer
intid = Page.Request("id").ToString
Dim dv As DataView = mysql.getdata(intid, "bbsre.xml")
datalist2.DataSource = dv
datalist2.DataBind()
End Sub
End Class