ClassをXML形式で、ファイルに書き込むまたは、読み込む方法を記載します。

読み書きするClassの例

Public Class Parameter
    Public val1 As String
    Public val2 As Double
    Public val3 As Integer
End Class

ClassをXMLファイルに書き込む

Dim FilePath As String = "C:\work\myfile.xml"

Dim prm As Parameter = New Parameter()
prm.val1 = "AAAAAAAAAAA"
prm.val2 = 20.1
prm.val3 = 10

Using sw As IO.StreamWriter = New IO.StreamWriter(FilePath, False, System.Text.Encoding.Default)
    Dim serializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(prm.GetType())
    serializer.Serialize(sw, prm)
End Using

 

XMLファイルをClassに読み込む

Dim FilePath As String = "C:\work\myfile.xml"

Dim prm As Parameter = New Parameter()
If IO.File.Exists(FilePath) Then
    Using sr As IO.StreamReader = New IO.StreamReader(FilePath, System.Text.Encoding.Default)
        Dim serializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(prm.GetType())
        prm = serializer.Deserialize(sr)
    End Using
End If