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">Ob ject.</param>
''' <returns>System .String</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function SerializeObject ToString(ByVal value As Object) As
System.String
Dim StringWriter As New System.IO.Strin gWriter
Try
Dim XmlSerializer As New
System.XML.Seri alization.XmlSe rializer(value. GetType)
XmlSerializer.S erialize(String Writer, value)
Return StringWriter.To String
Catch ex As Exception
Throw ex
Finally
StringWriter.Cl ose()
End Try
End Function
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes an object to a string.
''' </summary>
''' <param name="value">Ob ject.</param>
''' <param name="extraType s">System.Ty pe array.</param>
''' <returns>System .String</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function SerializeObject ToString(ByVal value As Object,
ByVal extraTypes() As System.Type) As System.String
Dim StringWriter As New System.IO.Strin gWriter
Try
Dim XmlSerializer As New
System.XML.Seri alization.XmlSe rializer(value. GetType, extraTypes)
XmlSerializer.S erialize(String Writer, value)
Return StringWriter.To String
Catch ex As Exception
Throw ex
Finally
StringWriter.Cl ose()
End Try
End Function
''' ------------------------------------------------------------------------
-----
''' <summary>
''' Serializes a object to a file.
''' </summary>
''' <param name="fileName" >String. File name and path.</param>
''' <param name="value">Ob ject. The object to serialize.</param>
''' <param name="extraType s">System.Ty pe array.</param>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Sub SerializeObject ToFile(ByVal fileName As String, ByVal
value As Object, ByVal extraTypes() As System.Type)
Dim writer As New StreamWriter(fi leName)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serializati on.XmlSerialize r(value.GetType , extraTypes)
' Serialize the object, and close the StreamWriter.
s.Serialize(wri ter, value)
Catch x As System.InvalidO perationExcepti on
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">Ob ject. The object to serialize.</param>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Sub SerializeObject ToFile(ByVal fileName As String, ByVal
value As Object)
Dim writer As New StreamWriter(fi leName)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serializati on.XmlSerialize r(value.GetType )
' Serialize the object, and close the StreamWriter.
s.Serialize(wri ter, value)
Catch x As System.InvalidO perationExcepti on
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="extraType s">System.Ty pe array.</param>
''' <returns>Object .</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/19/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObje ctFromFile(ByVa l fileName As String,
ByVal value As Object, ByVal extraTypes() As System.Type) As Object
Dim fs As New IO.FileStream(f ileName, FileMode.Open)
Try
Dim w As New Xml.Serializati on.XmlSerialize r(value.GetType , extraTypes)
Dim g As Object = w.Deserialize(f s)
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 DeserializeObje ctFromFile(ByVa l fileName As String,
ByVal value As Object) As Object
Dim fs As New IO.FileStream(f ileName, FileMode.Open)
Try
Dim w As New Xml.Serializati on.XmlSerialize r(value.GetType )
Dim g As Object = w.Deserialize(f s)
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">St ring.</param>
''' <param name="type">Obj ect.</param>
''' <returns>Object .</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/30/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObje ctFromString(By Val value As String,
ByVal type As Object) As Object
Dim fs As New IO.StringReader (value)
Try
Dim w As New Xml.Serializati on.XmlSerialize r(type.GetType)
Dim g As Object = w.Deserialize(f s)
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">St ring.</param>
''' <param name="type">Obj ect.</param>
''' <param name="extraType s">System.Ty pe array.</param>
''' <returns>Object .</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0] 9/30/2004 Created
''' </history>
''' ------------------------------------------------------------------------
-----
Public Shared Function DeserializeObje ctFromString(By Val 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.Serializati on.XmlSerialize r(type.GetType, extraTypes)
Dim g As Object = w.Deserialize(f s)
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******** ******@tk2msftn gp13.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.Seri alization.XmlSe rializer = _
New System.Xml.Seri alization.XmlSe rializer(xmlCla ss.GetType)
Dim sr As New System.IO.Strea mReader(path)
' Calls the Deserialize method and casts to the object type.
myObject = CType(xs.Deseri alize(sr), mySettings)
sr.Close()
Return myObject
End Function
I want to remove any mention of mySettings from the above.
Any ideas?
Matthew