A simple and effective configuration file reading method
Disclaimer: My "article" is only suitable for beginners. I understand the concept of understanding, and the reference only should be referred to the idea.
(I am afraid to be awkward, the statement is before)
Main practice:
The first step is to make the configuration information a simple class.
Key points: Because of serialization, and use XMLSerializer to serialize, the variable information to save to the file must be declared as public, and to serialize the class
Example: A configuration information for connecting the database
'Configuration information
'Initial default
Public ID as integer = 0
Public server as string = "(local)"
Public Database As String = "Northwind"
Public Tusted as Boolean = TRUE
Public user as string = "" "
Public Password As String = ""
END CLASS
Step 2: Configure the class.
Description: This class is universal. With other configurations, you only need to modify the above configuration information classes. It implements the function of archiving and reading, and translates into configuration information objects, which is more convenient to operate. You can add content encryption.
'Default ConnectionInfo ID
Public ID as integer
'Must be public, XMLSerializer only for Pulbic variables
Public items as item ()
PRIVATE M_COUNT AS INTEGER = 0
'Adding configuration information
Public Sub Add (ByVal ConnectionInfo As Item)
IF connectioninfo is nothing the eXIX
IF m_count> 0 THEN
IF not me.connectioninfo (connectioninfo.id) is nothing then
Msgbox (id.tostring & "already existing")
EXIT SUB
END IF
END IF
Redim preserve me.Items (m_count)
Me.Items (m_count) = ConnectionInfo
m_count = 1
End Sub
'Take the configuration information of the specified ID
Public Readonly Property ConnectionInfo (Byval ID As Integer) AS ITEM
Get
DIM MITEM AS ITEM
DIM I as integer
For i = 0 to m_count - 1
IF me.Items (i) .id = ID THEN
mitem = me.Items (ID)
EXIT for
END IF
NEXT
Return MItem
END GET
End Property
'Serialization and save to the specified file
Public Sub Savexmlfile (Byval FileName As String)
IF me is nothing kil
DIM XMLWRITER AS New System.io.Streamwriter (FileName, False)
XMLWRITER.WRITE (SERIALIZECLASS.GETXML (ME) XMLWriter.close ()
End Sub
'Read deserialization from specified files to objects
Public Function LoadXmlFile (Byval FileName As String) AS Config
Dim Xmlreader as new system.io.streamreader (filename, system.text.Encoding.default)
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
The above two steps can be, and this class is used to serialize.
'This is a class that uses XMLSerializer serialized objects.
Public Class SerializationClass
Public Shared Function GetXML (Byval Mobject As Object) AS STRING
Dim Osterializer As New System.xml.Serialization.xmlSerializer (MOBJECT.GETTYPE)
Dim OstringWriter as new system.io.stringwriter
Osterializer.Serialize (OstringWriter, MOBJECT)
Return OstringWriter.toString
END FUNCTION
Public Shared Function LoadXML (Byval McLass As Type, Byval XML AS String) AS Object
Dim Osterializer As New System.xml.Serialization.xmlSerializer (McLAS)
Dim OstringReader As New System.io.StringReader (XML)
Return Osterializer.deSerialize (OstringReader)
END FUNCTION
END CLASS
The following is an example of using:
'Test storage profile
Private Sub TestsaveConfig ()
DIM MCONFIG AS config = new config
DIM MITEM AS ITEM
mitem = new item
Mconfig.Add (MItem)
mitem = new item
With mitem
.Id = 1
.User = "master"
.Password = "123456"
.Tusted = false
End with
Mconfig.Add (MItem)
Mconfig.savexmlfile (Application.Startuppath & "/Config.xml")
End Sub
'Test read configuration file
Private sub testloadConfig ()
DIM MCONFIG AS config = new config
Mconfig = Mconfig.LoadXmlfile (Application.Startuppath & "/ Config.xml")
'Read the username of the default value
MsgBox (Mconfig.ID) .User "may wish to modify the configuration
Mconfig.ID = 1
Mconfig.connectionInfo (0) .server = "server"
Mconfig.connectionInfo (1) .DATABASE = "Master"
'Re-deposit disk
Mconfig.savexmlfile (Application.Startuppath & "/Config.xml")
End Sub