Advertisement
5_2007-2008 Files #179057

Saving and Loading Data with Serialization -- cooooool

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

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
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) 
Original Comments (3)
Recovered from Wayback Machine