Public Class XmlSerializer
Private _Object As Object
Private _TargetFile As String
''' <summary>
''' Gets or sets the System.Object to perform serialization/deserialization on.
''' </summary>
Public Property [Object]() As Object
Get
Return _Object
End Get
Set(ByVal value As Object)
_Object = value
End Set
End Property
''' <summary>
''' Gets or sets the System.String indicating where to serialize/deserialize.
''' </summary>
Public Property TargetFile() As String
Get
Return _TargetFile
End Get
Set(ByVal value As String)
_TargetFile = value
End Set
End Property
''' <summary>
''' Instantiates a new instance of the XmlSerializer class.
''' </summary>
''' <param name="Object">The System.Object to perform serialization/deserialization on.</param>
Public Sub New(ByVal [Object] As Object)
With Me
.Object = [Object]
End With
End Sub
''' <summary>
''' Instantiates a new instance of the XmlSerializer class.
''' </summary>
''' <param name="Object">The System.Object to perform serialization/deserialization on.</param>
''' <param name="TargetFile">The System.String indicating where to serialize/deserialize.</param>
Public Sub New(ByVal [Object] As Object, ByVal TargetFile As String)
With Me
.Object = [Object]
.TargetFile = TargetFile
End With
End Sub
''' <summary>
''' Performs serialization on Me.Object and writes to Me.TargetFile.
''' </summary>
''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
''' <returns>Boolean. Indicates operation success.</returns>
Public Function Serialize() As Boolean
If Not Me.Object.GetType.IsSerializable Then
Throw New Xml.XmlException("Value 'Object' must be serializable.")
End If
Try
Using StreamWriter As New System.IO.StreamWriter(Me.TargetFile)
Dim XmlSerializer As New System.Xml.Serialization.XmlSerializer(Me.Object.GetType)
XmlSerializer.Serialize(StreamWriter, Me.Object)
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' Performs serialization on Me.Object and writes to the passed System.String.
''' </summary>
''' <param name="TargetFile">The System.String indicating where to serialize.</param>
''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
''' <returns>Boolean. Indicates operation success.</returns>
Public Function Serialize(ByVal TargetFile As String) As Boolean
If Not Me.Object.GetType.IsSerializable Then
Throw New Xml.XmlException("Value 'Object' must be serializable.")
End If
Try
Using StreamWriter As New System.IO.StreamWriter(TargetFile)
Dim XmlSerializer As New System.Xml.Serialization.XmlSerializer(Me.Object.GetType)
XmlSerializer.Serialize(StreamWriter, Me.Object)
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' Performs serialization on the passed System.Object and writes to the passed System.String.
''' </summary>
''' <param name="Object">The System.Object to perform serialization on.</param>
''' <param name="TargetFile">The System.String indicating where to serialize.</param>
''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
''' <returns>Boolean. Indicates operation success.</returns>
Public Shared Function Serialize(ByVal [Object] As Object, ByVal TargetFile As String) As Boolean
If Not [Object].GetType.IsSerializable Then
Throw New Xml.XmlException("Value 'Object' must be serializable.")
End If
Try
Using StreamWriter As New System.IO.StreamWriter(TargetFile)
Dim XmlSerializer As New System.Xml.Serialization.XmlSerializer([Object].GetType)
XmlSerializer.Serialize(StreamWriter, [Object])
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' Performs deserialization on Me.Object and reads from Me.TargetFile.
''' </summary>
''' <returns>Object. Represents the deserialized System.Object if operation success, otherwise returns Nothing.</returns>
''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
''' <exception cref="IO.FileNotFoundException">Thrown if the System.String indicating the file path does not exist.</exception>
Public Function Deserialize() As Object
If Not Me.Object.GetType.IsSerializable Then
Throw New Xml.XmlException("Value 'Object' must be serializable.")
ElseIf Not My.Computer.FileSystem.FileExists(Me.TargetFile) Then
Throw New IO.FileNotFoundException("Value 'TargetFile' must be a file that exists.")
End If
Try
Using StreamReader As New System.IO.StreamReader(Me.TargetFile)
Dim XmlSerializer As New Xml.Serialization.XmlSerializer(Me.Object.GetType)
Return XmlSerializer.Deserialize(StreamReader)
End Using
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' Performs deserialization on Me.Object and reads from the passed System.String.
''' </summary>
''' <param name="TargetFile">The System.String indicating where to deserialize from.</param>
''' <returns>Object. Represents the deserialized System.Object if operation success, otherwise returns Nothing.</returns>
''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
''' <exception cref="IO.FileNotFoundException">Thrown if the System.String indicating the file path does not exist.</exception>
Public Function Deserialize(ByVal TargetFile As String) As Object
If Not Me.Object.GetType.IsSerializable Then
Throw New Xml.XmlException("Value 'Object' must be serializable.")
ElseIf Not My.Computer.FileSystem.FileExists(TargetFile) Then
Throw New IO.FileNotFoundException("Value 'TargetFile' must be a file that exists.")
End If
Try
Using StreamReader As New System.IO.StreamReader(TargetFile)
Dim XmlSerializer As New Xml.Serialization.XmlSerializer(Me.Object.GetType)
Return XmlSerializer.Deserialize(StreamReader)
End Using
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' Performs deserialization on the passed System.Object and reads from the passed System.String.
''' </summary>
''' <param name="TargetFile">The System.String indicating where to deserialize from.</param>
''' <param name="Object">The System.Object to perform deserialization on.</param>
''' <returns>Object. Represents the deserialized System.Object if operation success, otherwise returns Nothing.</returns>
''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
''' <exception cref="IO.FileNotFoundException">Thrown if the System.String indicating the file path does not exist.</exception>
Public Shared Function Deserialize(ByVal [Object] As Object, ByVal TargetFile As String) As Object
If Not [Object].GetType.IsSerializable Then
Throw New Xml.XmlException("Value 'Object' must be serializable.")
ElseIf Not My.Computer.FileSystem.FileExists(TargetFile) Then
Throw New IO.FileNotFoundException("Value 'TargetFile' must be a file that exists.")
End If
Try
Using StreamReader As New System.IO.StreamReader(TargetFile)
Dim XmlSerializer As New Xml.Serialization.XmlSerializer([Object].GetType)
Return XmlSerializer.Deserialize(StreamReader)
End Using
Catch ex As Exception
Return Nothing
End Try
End Function
End Class