VB icon

Save and Load Objects from XML Files

Email
Submitted on: 1/5/2015 11:59:00 AM
By: Kamilche (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: VB.NET
Views: 2160
 
     This example illustrates how to save objects to XML files, and load objects from XML files, via serialization.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Save and Load Objects from XML Files
// Description:This example illustrates how to save objects to XML files, and load objects from XML files, via serialization.
// By: Kamilche (from psc cd)
//
// Returns:Sample output:
Original values in XML file C:\Documents and Settings\Administrator\Local Settings\Temp\tmp45A5.tmp:
name = Cindy, age = 45, happy = True, color = (red=0, green=0, blue=0, alpha=0)
name = Kevin, age = 44, happy = True, color = (red=0, green=0, blue=0, alpha=255)
name = David, age = 18, happy = True, color = (red=255, green=0, blue=0, alpha=128)
Loaded values from XML file C:\Documents and Settings\Administrator\Local Settings\Temp\tmp45A5.tmp:
name = Cindy, age = 45, happy = True, color = (red=0, green=0, blue=0, alpha=0)
name = Kevin, age = 44, happy = True, color = (red=0, green=0, blue=0, alpha=255)
name = David, age = 18, happy = True, color = (red=255, green=0, blue=0, alpha=128)
Contents of XML file:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Customer>
<name>Cindy</name>
<age>45</age>
<happy>true</happy>
<color>
 <red>0</red>
 <green>0</green>
 <blue>0</blue>
 <alpha>0</alpha>
</color>
 </Customer>
 <Customer>
<name>Kevin</name>
<age>44</age>
<happy>true</happy>
<color>
 <red>0</red>
 <green>0</green>
 <blue>0</blue>
 <alpha>255</alpha>
</color>
 </Customer>
 <Customer>
<name>David</name>
<age>18</age>
<happy>true</happy>
<color>
 <red>255</red>
 <green>0</green>
 <blue>0</blue>
 <alpha>128</alpha>
</color>
 </Customer>
</ArrayOfCustomer>
//**************************************

Imports System.Xml.Serialization
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Put a breakpoint on the next line and step through it.
Test()
End Sub
Public Sub Test()
Dim filename As String = My.Computer.FileSystem.GetTempFileName()
Dim lst As List(Of Customer)
Dim lst2 As List(Of Customer)
' Create list of objects
lst = New List(Of Customer)
lst.Add(New Customer("Cindy", 45, True, New MyColor(0, 0, 0, 0)))
lst.Add(New Customer("Kevin", 44, True, New MyColor(0, 0, 0, 255)))
lst.Add(New Customer("David", 18, True, New MyColor(255, 0, 0, 128)))
' Print original values
Debug.Print(vbCrLf & "Original values in XML file " & filename & ":")
For Each item As Customer In lst
Debug.Print("" & item.ToString)
Next
' Save to XML file
SaveObjectToXML(Of List(Of Customer))(lst, filename)
' Load from XML file
lst2 = LoadObjectFromXML(Of List(Of Customer))(filename)
' Print loaded values
Debug.Print(vbCrLf & "Loaded values from XML file " & filename & ":")
For Each item As Customer In lst2
Debug.Print("" & item.ToString)
Next
' Print XML file
Debug.Print(vbCrLf & "Contents of XML file:")
Debug.Print(File.ReadAllText(filename))
End Sub
End Class
Public Module modMain
Public Function SaveObjectToXML(Of T)(ByVal obj As Object, ByVal filename As String) As Boolean
' Save an object to XML.
' Usage: SaveObjectToXML(Of Project)(proj, filename)
Dim sw As StreamWriter = New StreamWriter(filename)
Dim xmls As XmlSerializer = Nothing
Try
xmls = New XmlSerializer(GetType(T))
xmls.Serialize(sw, obj)
Catch ex As Exception
Debug.Print(ex.ToString)
MsgBox(ex.ToString)
Return False
End Try
sw.Flush()
sw.Close()
Return True
End Function
Public Function LoadObjectFromXML(Of T)(ByVal filename As String) As T
' Load an object from XML.
' Usage: proj = LoadObjectFromXML(Of Project)(filename)
Dim sr As StreamReader = New StreamReader(filename)
Dim xmls As XmlSerializer = Nothing
Dim obj As T
Try
xmls = New XmlSerializer(GetType(T))
obj = CType(xmls.Deserialize(sr), T)
Catch ex As Exception
Debug.Print(ex.ToString)
MsgBox(ex.ToString)
Return Nothing
End Try
sr.Close()
Return obj
End Function
End Module
' Classes must be public and reside outside any modules to be serializable
<Serializable()> _
Public Class Customer
Public name As String
Public age As Integer
Public happy As Boolean
Public color As MyColor
Public Sub New(ByVal name As String, ByVal age As Integer, ByVal happy As Boolean, ByVal color As MyColor)
Me.name = name
Me.age = age
Me.happy = happy
Me.color = color
End Sub
Public Sub New()
' Must have a parameterless constructor to work
End Sub
Public Overrides Function ToString() As String
Return "name = " & name & ", age = " & age & ", happy = " & happy & ", color = " & color.ToString
End Function
End Class
Public Class MyColor
Public red As Byte
Public green As Byte
Public blue As Byte
Public alpha As Byte
Public Sub New(ByVal red As Byte, ByVal green As Byte, ByVal blue As Byte, ByVal alpha As Byte)
Me.red = red
Me.green = green
Me.blue = blue
Me.alpha = alpha
End Sub
Public Sub New()
'necessary for seralization
End Sub
Public Overrides Function ToString() As String
Return "(red=" & red & ", green=" & green & ", blue=" & blue & ", alpha=" & alpha & ")"
End Function
End Class


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.