Imports System.IO
Imports System.Xml.Serialization
Public Module Module1
Sub Main()
Dim cc As clsCustomConfig = New clsCustomConfig()
cc.CellConfigList = New List(Of clsCellConfig)
cc.CellConfigList.Add(New clsCellConfig() With {.CellID = 1, .RsrpMax = 100, .RsrpMin = 90})
cc.CellConfigList.Add(New clsCellConfig() With {.CellID = 2, .RsrpMax = 100, .RsrpMin = 90})
cc.CellConfigList.Add(New clsCellConfig() With {.CellID = 3, .RsrpMax = 100, .RsrpMin = 90})
cc.Data_Enable = True
cc.MO_Enable = True
cc.MT_Enanble = True
cc.StepNumber = 1
Dim filePath = Environment.CurrentDirectory + "\test.xml"
If SaveConfigToFile(Of clsCustomConfig)(filePath, cc) Then
Console.WriteLine("Save successfully")
End If
Dim temp = New clsCustomConfig
If ReadConfigFromFile(Of clsCustomConfig)(filePath, temp) Then
Console.WriteLine("read successfully")
End If
Console.Read()
End Sub
Public Class clsCellConfig
Public Property CellID As Integer
Public Property RsrpMax As Double
Public Property RsrpMin As Double
End Class
Public Class clsCustomConfig
Public Property CellConfigList As List(Of clsCellConfig)
Public Property Data_Enable As Boolean
Public Property MO_Enable As Boolean
Public Property MT_Enanble As Boolean
Public Property StepNumber As Integer
End Class
Public Function SaveConfigToFile(Of T)(ByVal strFilePath As String, ByVal data As T) As Boolean
Dim objStreamWriter As New StreamWriter(strFilePath)
Try
Dim xsSerialize As New XmlSerializer(data.GetType)
xsSerialize.Serialize(objStreamWriter, data)
objStreamWriter.Close()
Catch ex As Exception
objStreamWriter.Close()
Return False
End Try
Return True
End Function
Public Function ReadConfigFromFile(Of T)(ByVal strFilePath As String, ByRef data As T) As Boolean
Dim objStreamReader As New StreamReader(strFilePath)
Try
Dim xsDeserialize As New XmlSerializer(data.GetType)
data = xsDeserialize.Deserialize(objStreamReader)
objStreamReader.Close()
Catch ex As Exception
objStreamReader.Close()
Return False
End Try
Return True
End Function
End Module