主题:  ASPNET中,从XML文件中读取站点信息的方法

蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
#12004/10/28 17:07:43
我以前做了一个留言板系统,比较简单。其中用到从磁盘中读取基本信息的方法,如站名,LOGO等。如果每个页面的打开都要读一遍信息,将对服务器造成负担。所以结合了Session,从磁盘中读取一次信息,然后把信息存在Session中。页面要用到信息是,先判断Session中是否有需要的内容,如果有,则调用Session,没有再从磁盘读取。比较简单,因这里开始学.NET朋友较多,所以从简单开始吧。

先建立XML文件,存放站点信息:

webinfo.xml

<?xml version="1.0" encoding="gb2312" ?>
<系统信息>
    <基本信息>
    <留言簿名称>水晶留言系统单用户版</留言簿名称>
    <站长>bluewhale</站长>
    <密码>21232F297A57A5A743894A0E4A801FC3</密码>
    <站长信箱>bluewhale@hellosea.com</站长信箱>
    <版权><![CDATA[Copyright 2004 <a href="http://www.hellosea.com" target="_blank">hellosea.com</a> All Rights Reserved 水晶工作室版权所有]]></版权>

    <首页地址>http://www.hellosea.com</首页地址>
    <LOGO>images/logo.gif</LOGO>
    <公告栏>欢迎使用基于ASP.NET技术的留言簿,如果您有什么建议或问题请和我联系。</公告栏>

    ......

    </基本信息>
</系统信息>

调用基本信息的,我把它包装成一个类,内容如下:

Public Class UWebinfo
    Inherits System.Web.UI.Page

    Private webName As String
    Private webAdmin As String
    Private webAdminPassword As String
    Private webEmail As String
    Private webCopyright As String

    Private webHomesite As String
    Private webLogo As String
    Private webAffiche As String

    Private webAllowHtml As String
    Private webAllowShowIP As String
    Private webSpaceTime As String
    Private webContentMaxLength As String
    Private webPagesize As String

    ......

#Region " 成员 "

    Property Name() As String
        Get
            Return webName
        End Get
        Set(ByVal value As String)
            webName = value
        End Set
    End Property

    Property Admin() As String
        Get
            Return webAdmin
        End Get
        Set(ByVal value As String)
            webAdmin = value
        End Set
    End Property

    Property AdminPassword() As String
        Get
            Return webAdminPassword
        End Get
        Set(ByVal value As String)
            webAdminPassword = value
        End Set
    End Property

    ......'太多了,不再废话
#End Region

    Public Sub New()
        ReadInfo(Server.MapPath("WebInfo.xml"))
    End Sub

    '判断session中有没有值
    Private Function NullSession() As Boolean
        If Convert.ToString(Session("webName")) = "" Or _
         Convert.ToString(Session("webAdmin")) = "" Or _
         Convert.ToString(Session("webAdminPassword")) = "" Or _
         Convert.ToString(Session("webEmail")) = "" Or _
         Convert.ToString(Session("webCopyright")) = "" Or _
         Convert.ToString(Session("webHomesite")) = "" Or _
         Convert.ToString(Session("webLogo")) = "" Or _
         Convert.ToString(Session("webAllowHtml")) = "" Or _
         Convert.ToString(Session("webAllowShowIP")) = "" Or _
         Convert.ToString(Session("webSpaceTime")) = "" Or _
         Convert.ToString(Session("webContentMaxLength")) = "" Or _
         ......
         Convert.ToString(Session("webPagesize")) = "" Then

            Return True
        Else
            Return = False
        End If
    End Function

    '读取常规信息
    Public Function ReadInfo(ByVal strFile As String)
        If Not NullSession() Then
            webName = Session("webname")
            webAdmin = Session("webAdmin")
            webAdminPassword = Session("webAdminPassword")
            webEmail = Session("webEmail")
            webCopyright = Session("webCopyright")

            webHomesite = Session("webHomesite")
            webLogo = Session("webLogo")
            webAffiche = Session("webAffiche")

            ......
        Else
            Dim ds As New DataSet
            ds.ReadXml(strFile)

            webName = ds.Tables(0).Rows(0)("留言簿名称")
            webAdmin = ds.Tables(0).Rows(0)("站长")
            webAdminPassword = ds.Tables(0).Rows(0)("密码")
            webEmail = ds.Tables(0).Rows(0)("站长信箱")
            webCopyright = ds.Tables(0).Rows(0)("版权")

            webHomesite = ds.Tables(0).Rows(0)("首页地址")
            webLogo = ds.Tables(0).Rows(0)("LOGO")
            webAffiche = ds.Tables(0).Rows(0)("公告栏")

            ......

            ds.Dispose()
            
            ' 读出的信息就该放到Session中
            Session("webname") = webName
            Session("webAdmin") = webAdmin
            Session("webAdminPassword") = webAdminPassword
            Session("webEmail") = webEmail
            Session("webCopyright") = webCopyright

            Session("webHomesite") = webHomesite
            Session("webLogo") = webLogo
            Session("webAffiche") = webAffiche

            Session("webAllowHtml") = webAllowHtml
            Session("webAllowShowIP") = webAllowShowIP
            Session("webSpaceTime") = webSpaceTime
            Session("webContentMaxLength") = webContentMaxLength
            Session("webPagesize") = webPagesize

            Session("webStyleInex") = webStyleInex.ToString
        End If
    End Function    

End Class

比较基础的,但设计已经是面向对象的设计。调用方法:

Dim WebInfo As UWebinfo = New UWebinfo()
txtName.Text = WebInfo.Name
txtAdmin.Text = WebInfo.Admin
......

编辑历史:[此帖最近一次被 蓝鲸 编辑过(编辑时间:2004-10-28 20:26:00)]

非常大鱼