Well you should seperate the Serialize and DeSerialize into different
functions.
Other tips:
Always use (Unless not possible):
Option Explicit On
Option Strict On
User DirectCast instead of Ctype when possible.
Here's some samples below (See SerializeToFile)
I also have some more advanced demos on
www.CodeProject.com
Jusr look in the XML section
'Copyright © 2004 Eric Schneider
Option Explicit On
Option Strict On
Imports System
Imports System.Xml
Imports System.IO
Public NotInheritable Class PersistenceBase
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes an object to a string.
''' </summary>
''' <param name="value">Object.</param>
''' <returns>System.String</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function SerializeObjectToString(ByVal value As Object) As
System.String
Dim StringWriter As New System.IO.StringWriter
Try
Dim XmlSerializer As New
System.XML.Serialization.XmlSerializer(value.GetTy pe)
XmlSerializer.Serialize(StringWriter, value)
Return StringWriter.ToString
Catch ex As Exception
Throw ex
Finally
StringWriter.Close()
End Try
End Function
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes an object to a string.
''' </summary>
''' <param name="value">Object.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <returns>System.String</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function SerializeObjectToString(ByVal value As Object,
ByVal extraTypes() As System.Type) As System.String
Dim StringWriter As New System.IO.StringWriter
Try
Dim XmlSerializer As New
System.XML.Serialization.XmlSerializer(value.GetTy pe, extraTypes)
XmlSerializer.Serialize(StringWriter, value)
Return StringWriter.ToString
Catch ex As Exception
Throw ex
Finally
StringWriter.Close()
End Try
End Function
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes a object to a file.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <param name="value">Object. The object to serialize.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Sub SerializeObjectToFile(ByVal fileName As String, ByVal
value As Object, ByVal extraTypes() As System.Type)
Dim writer As New StreamWriter(fileName)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(value.GetType, extraTypes)
' Serialize the object, and close the StreamWriter.
s.Serialize(writer, value)
Catch x As System.InvalidOperationException
Throw x
Finally
writer.Close()
End Try
End Sub
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes a object to a file.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <param name="value">Object. The object to serialize.</param>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Sub SerializeObjectToFile(ByVal fileName As String, ByVal
value As Object)
Dim writer As New StreamWriter(fileName)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(value.GetType)
' Serialize the object, and close the StreamWriter.
s.Serialize(writer, value)
Catch x As System.InvalidOperationException
Throw x
Finally
writer.Close()
End Try
End Sub
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a file into a object.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromFile(ByVal fileName As String,
ByVal value As Object, ByVal extraTypes() As System.Type) As Object
Dim fs As New IO.FileStream(fileName, FileMode.Open)
Try
Dim w As New Xml.Serialization.XmlSerializer(value.GetType, extraTypes)
Dim g As Object = w.Deserialize(fs)
Return g
Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a file into a object.
''' </summary>
''' <param name="fileName">String. File name and path.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromFile(ByVal fileName As String,
ByVal value As Object) As Object
Dim fs As New IO.FileStream(fileName, FileMode.Open)
Try
Dim w As New Xml.Serialization.XmlSerializer(value.GetType)
Dim g As Object = w.Deserialize(fs)
Return g
Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a String into a object.
''' </summary>
''' <param name="value">String.</param>
''' <param name="type">Object.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/30/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromString(ByVal value As String,
ByVal type As Object) As Object
Dim fs As New IO.StringReader(value)
Try
Dim w As New Xml.Serialization.XmlSerializer(type.GetType)
Dim g As Object = w.Deserialize(fs)
Return g
Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Deserializes a String into a object.
''' </summary>
''' <param name="value">String.</param>
''' <param name="type">Object.</param>
''' <param name="extraTypes">System.Type array.</param>
''' <returns>Object.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/30/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObjectFromString(ByVal value As String,
ByVal type As Object, ByVal extraTypes() As System.Type) As Object
Dim fs As New IO.StringReader(value)
Try
Dim w As New Xml.Serialization.XmlSerializer(type.GetType, extraTypes)
Dim g As Object = w.Deserialize(fs)
Return g
Catch x As Exception
Throw x
Finally
fs.Close()
End Try
End Function
Private Sub New()
End Sub
End Class
"Matthew" <tu*************@alltel.net> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
I thought I was done, but there is one more twist.
Public Function xmlLoad(ByVal path As String, _
ByVal xmlClass As Object)
Dim myObject As New mySettings
Dim xs As System.Xml.Serialization.XmlSerializer = _
New System.Xml.Serialization.XmlSerializer(xmlClass.Ge tType)
Dim sr As New System.IO.StreamReader(path)
' Calls the Deserialize method and casts to the object type.
myObject = CType(xs.Deserialize(sr), mySettings)
sr.Close()
Return myObject
End Function
I want to remove any mention of mySettings from the above.
Any ideas?
Matthew