Results 1 to 1 of 1

Thread: [VB.Net 2.0+] Basic Save Functionality (System.Xml.Serialization.XmlSerializer)

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    [.NET 2.0+] Basic Save Functionality (System.Xml.Serialization.XmlSerializer)

    I'm not sure who will benefit by this, but I created a Serialize/Deserialize class for the function of saving user settings/configurations etc... I fully commented the code with Xml.

    Code:

    vb.net Code:
    1. Public Class XmlSerializer
    2.     Private _Object As Object
    3.     Private _TargetFile As String
    4.     ''' <summary>
    5.     ''' Gets or sets the System.Object to perform serialization/deserialization on.
    6.     ''' </summary>
    7.     Public Property [Object]() As Object
    8.         Get
    9.             Return _Object
    10.         End Get
    11.         Set(ByVal value As Object)
    12.             _Object = value
    13.         End Set
    14.     End Property
    15.     ''' <summary>
    16.     ''' Gets or sets the System.String indicating where to serialize/deserialize.
    17.     ''' </summary>
    18.     Public Property TargetFile() As String
    19.         Get
    20.             Return _TargetFile
    21.         End Get
    22.         Set(ByVal value As String)
    23.             _TargetFile = value
    24.         End Set
    25.     End Property
    26.     ''' <summary>
    27.     ''' Instantiates a new instance of the XmlSerializer class.
    28.     ''' </summary>
    29.     ''' <param name="Object">The System.Object to perform serialization/deserialization on.</param>
    30.     Public Sub New(ByVal [Object] As Object)
    31.         With Me
    32.             .Object = [Object]
    33.         End With
    34.     End Sub
    35.     ''' <summary>
    36.     ''' Instantiates a new instance of the XmlSerializer class.
    37.     ''' </summary>
    38.     ''' <param name="Object">The System.Object to perform serialization/deserialization on.</param>
    39.     ''' <param name="TargetFile">The System.String indicating where to serialize/deserialize.</param>
    40.     Public Sub New(ByVal [Object] As Object, ByVal TargetFile As String)
    41.         With Me
    42.             .Object = [Object]
    43.             .TargetFile = TargetFile
    44.         End With
    45.     End Sub
    46.     ''' <summary>
    47.     ''' Performs serialization on Me.Object and writes to Me.TargetFile.
    48.     ''' </summary>
    49.     ''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
    50.     ''' <returns>Boolean. Indicates operation success.</returns>
    51.     Public Function Serialize() As Boolean
    52.         If Not Me.Object.GetType.IsSerializable Then
    53.             Throw New Xml.XmlException("Value 'Object' must be serializable.")
    54.         End If
    55.         Try
    56.             Using StreamWriter As New System.IO.StreamWriter(Me.TargetFile)
    57.                 Dim XmlSerializer As New System.Xml.Serialization.XmlSerializer(Me.Object.GetType)
    58.                 XmlSerializer.Serialize(StreamWriter, Me.Object)
    59.             End Using
    60.             Return True
    61.         Catch ex As Exception
    62.             Return False
    63.         End Try
    64.     End Function
    65.     ''' <summary>
    66.     ''' Performs serialization on Me.Object and writes to the passed System.String.
    67.     ''' </summary>
    68.     ''' <param name="TargetFile">The System.String indicating where to serialize.</param>
    69.     ''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
    70.     ''' <returns>Boolean. Indicates operation success.</returns>
    71.     Public Function Serialize(ByVal TargetFile As String) As Boolean
    72.         If Not Me.Object.GetType.IsSerializable Then
    73.             Throw New Xml.XmlException("Value 'Object' must be serializable.")
    74.         End If
    75.         Try
    76.             Using StreamWriter As New System.IO.StreamWriter(TargetFile)
    77.                 Dim XmlSerializer As New System.Xml.Serialization.XmlSerializer(Me.Object.GetType)
    78.                 XmlSerializer.Serialize(StreamWriter, Me.Object)
    79.             End Using
    80.             Return True
    81.         Catch ex As Exception
    82.             Return False
    83.         End Try
    84.     End Function
    85.     ''' <summary>
    86.     ''' Performs serialization on the passed System.Object and writes to the passed System.String.
    87.     ''' </summary>
    88.     ''' <param name="Object">The System.Object to perform serialization on.</param>
    89.     ''' <param name="TargetFile">The System.String indicating where to serialize.</param>
    90.     ''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
    91.     ''' <returns>Boolean. Indicates operation success.</returns>
    92.     Public Shared Function Serialize(ByVal [Object] As Object, ByVal TargetFile As String) As Boolean
    93.         If Not [Object].GetType.IsSerializable Then
    94.             Throw New Xml.XmlException("Value 'Object' must be serializable.")
    95.         End If
    96.         Try
    97.             Using StreamWriter As New System.IO.StreamWriter(TargetFile)
    98.                 Dim XmlSerializer As New System.Xml.Serialization.XmlSerializer([Object].GetType)
    99.                 XmlSerializer.Serialize(StreamWriter, [Object])
    100.             End Using
    101.             Return True
    102.         Catch ex As Exception
    103.             Return False
    104.         End Try
    105.     End Function
    106.     ''' <summary>
    107.     ''' Performs deserialization on Me.Object and reads from Me.TargetFile.
    108.     ''' </summary>
    109.     ''' <returns>Object. Represents the deserialized System.Object if operation success, otherwise returns Nothing.</returns>
    110.     ''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
    111.     ''' <exception cref="IO.FileNotFoundException">Thrown if the System.String indicating the file path does not exist.</exception>
    112.     Public Function Deserialize() As Object
    113.         If Not Me.Object.GetType.IsSerializable Then
    114.             Throw New Xml.XmlException("Value 'Object' must be serializable.")
    115.         ElseIf Not My.Computer.FileSystem.FileExists(Me.TargetFile) Then
    116.             Throw New IO.FileNotFoundException("Value 'TargetFile' must be a file that exists.")
    117.         End If
    118.         Try
    119.             Using StreamReader As New System.IO.StreamReader(Me.TargetFile)
    120.                 Dim XmlSerializer As New Xml.Serialization.XmlSerializer(Me.Object.GetType)
    121.                 Return XmlSerializer.Deserialize(StreamReader)
    122.             End Using
    123.         Catch ex As Exception
    124.             Return Nothing
    125.         End Try
    126.     End Function
    127.     ''' <summary>
    128.     ''' Performs deserialization on Me.Object and reads from the passed System.String.
    129.     ''' </summary>
    130.     ''' <param name="TargetFile">The System.String indicating where to deserialize from.</param>
    131.     ''' <returns>Object. Represents the deserialized System.Object if operation success, otherwise returns Nothing.</returns>
    132.     ''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
    133.     ''' <exception cref="IO.FileNotFoundException">Thrown if the System.String indicating the file path does not exist.</exception>
    134.     Public Function Deserialize(ByVal TargetFile As String) As Object
    135.         If Not Me.Object.GetType.IsSerializable Then
    136.             Throw New Xml.XmlException("Value 'Object' must be serializable.")
    137.         ElseIf Not My.Computer.FileSystem.FileExists(TargetFile) Then
    138.             Throw New IO.FileNotFoundException("Value 'TargetFile' must be a file that exists.")
    139.         End If
    140.         Try
    141.             Using StreamReader As New System.IO.StreamReader(TargetFile)
    142.                 Dim XmlSerializer As New Xml.Serialization.XmlSerializer(Me.Object.GetType)
    143.                 Return XmlSerializer.Deserialize(StreamReader)
    144.             End Using
    145.         Catch ex As Exception
    146.             Return Nothing
    147.         End Try
    148.     End Function
    149.     ''' <summary>
    150.     ''' Performs deserialization on the passed System.Object and reads from the passed System.String.
    151.     ''' </summary>
    152.     ''' <param name="TargetFile">The System.String indicating where to deserialize from.</param>
    153.     ''' <param name="Object">The System.Object to perform deserialization on.</param>
    154.     ''' <returns>Object. Represents the deserialized System.Object if operation success, otherwise returns Nothing.</returns>
    155.     ''' <exception cref="Xml.XmlException">Thrown if the System.Object is not serializable.</exception>
    156.     ''' <exception cref="IO.FileNotFoundException">Thrown if the System.String indicating the file path does not exist.</exception>
    157.     Public Shared Function Deserialize(ByVal [Object] As Object, ByVal TargetFile As String) As Object
    158.         If Not [Object].GetType.IsSerializable Then
    159.             Throw New Xml.XmlException("Value 'Object' must be serializable.")
    160.         ElseIf Not My.Computer.FileSystem.FileExists(TargetFile) Then
    161.             Throw New IO.FileNotFoundException("Value 'TargetFile' must be a file that exists.")
    162.         End If
    163.         Try
    164.             Using StreamReader As New System.IO.StreamReader(TargetFile)
    165.                 Dim XmlSerializer As New Xml.Serialization.XmlSerializer([Object].GetType)
    166.                 Return XmlSerializer.Deserialize(StreamReader)
    167.             End Using
    168.         Catch ex As Exception
    169.             Return Nothing
    170.         End Try
    171.     End Function
    172. End Class

    Useage:

    vb.net Code:
    1. Public Class MyForm
    2.     Private cls As New MySerializableClass
    3.     Private Ser As New XmlSerializer(cls, "C:\Test\test.xml")
    4.     Private Sub SerializeMyClass()
    5.         cls.SomePropertyToSave = "A value saved is a... value saved?"
    6.         Ser.Serialize()
    7.     End Sub
    8.     Private Sub DeserializeMyClass()
    9.         cls = TryCast(Ser.Deserialize, MySerializableClass)
    10.         If cls IsNot Nothing Then
    11.             MessageBox.Show(cls.SomePropertyToSave)
    12.         End If
    13.     End Sub
    14. End Class
    15.  
    16. <System.Serializable()> Public Class MySerializableClass
    17.     Private _SomePropertyToSave As String
    18.     Public Property SomePropertyToSave() As String
    19.         Get
    20.             Return _SomePropertyToSave
    21.         End Get
    22.         Set(ByVal value As String)
    23.             _SomePropertyToSave = value
    24.         End Set
    25.     End Property
    26. End Class
    Last edited by ForumAccount; Aug 8th, 2011 at 05:20 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width