正在阅读:一种简便实效的配置文件读写方法一种简便实效的配置文件读写方法

2004-11-01 15:25 出处:CSDN 作者:lzmtw 责任编辑:linjixiong


       Dim mConfig As Config

        mConfig = CType(SerializeClass.LoadXML(GetType(Config), XmlReader.ReadToEnd), Config)

        XmlReader.Close()

        mConfig.m_Count = mConfig.Items.Length

        Return mConfig

    End Function

End Class

 

  就以上两步就可以了,下面这个类是用来序列化用的。


'这是用XmlSerializer序列化对象的类

Public Class SerializeClass

    Public Shared Function GetXML(ByVal mObject As Object) As String

        Dim oSerializer As New System.Xml.Serialization.XmlSerializer(mObject.GetType)

        Dim oStringWriter As New System.IO.StringWriter

        oSerializer.Serialize(oStringWriter, mObject)

        Return oStringWriter.ToString

    End Function

    Public Shared Function LoadXML(ByVal mClass As Type, ByVal XML As String) As Object

        Dim oSerializer As New System.Xml.Serialization.XmlSerializer(mClass)

        Dim oStringReader As New System.IO.StringReader(XML)

        Return oSerializer.Deserialize(oStringReader)

    End Function

End Class

  以下是使用方法示例:


    '测试存配置文件

    Private Sub TestSaveConfig()

        Dim mConfig As Config = New Config


察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品