README.md

December 5, 2022 ยท View on GitHub

Saving and Loading Data with Serialization -- cooooool

Description

Save entire classes or structs at a time to an xml file, and then read them back.

More Info

File to save as?

An Object.

Submitted On
ByJohnL
LevelIntermediate
User Rating4.4 (22 globes from 5 users)
CompatibilityVB.NET
CategoryFiles
World.Net (C#, VB.net)
Archive File

Source Code

Imports System.Xml.Serialization
Public Class Settings
  'or Struct
  'this is our class that holds all our settings.
  Public RandomSetting As Integer
  Public RandomString As String
  'etc.
End Class
  Public Sub SaveSettings(ByRef mypath As String, ByRef mySettings As Settings)
    Dim XmlS As New XmlSerializer(GetType(Settings))
    Dim fStream As New IO.FileStream(mypath, IO.FileMode.Create)
    XmlS.Serialize(fStream, mySettings)
    fStream.Close()
    fStream = Nothing
    XmlS = Nothing
  End Sub
  Public Function LoadSettings(ByRef mypath As String) As Settings
   Dim XmlS As New XmlSerializer(GetType(Settings))
    Dim fStream As New IO.FileStream(mypath, IO.FileMode.Open)
    Return XmlS.Deserialize(fStream)
  End Function
USAGE
 Dim R as New Settings() 'to save
 Dim S as New Settings() 'to load
 R.RandomSetting = 12
 R.RandomString = "~)!~~"
 SaveSettings("C:\mySettings.xml",R)
 S = LoadSettings("C:\mySettings.xml")
 Msgbox(S.RandomSetting & " " & S.RandomString)