|
VB.NET • Serialize Class Contents to XML Listing 1. Serializing and deserializing classes to XML is an easy way to save settings in .NET. Only the public data in the class structures is saved to the XML file, and you have fine-grained control over how the data elements are saved and retrieved. Class data embedded within another class is serialized automatically. This is a powerful capability you can take advantage of in other apps. Private Sub SaveSettings()
Dim fs As New FileStream(m_sSettingsPath, _
FileMode.OpenOrCreate)
Dim tw As TextWriter = New StreamWriter(fs)
Dim xs As New XmlSerializer _
(GetType(MonitorHeader))
xs.Serialize(fs, m_MonitorControl)
fs.Close()
End Sub
Private Sub LoadSettings()
m_MonitorControl = New MonitorHeader()
Dim fs As New FileStream(m_sSettingsPath, _
FileMode.Open)
Dim xs As New XmlSerializer _
(GetType(MonitorHeader))
m_MonitorControl = CType(xs.Deserialize(fs), _
MonitorHeader)
fs.Close()
End Sub
|