Results 1 to 3 of 3

Thread: [RESOLVED] Trying to Serialize a class

  1. #1

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

    Resolved [RESOLVED] Trying to Serialize a class

    I'm trying to serialize this class:

    Code:
    <System.Serializable()> Public Class Person 
        Private _FirstName As String 
        Public Property FirstName() As String 
            Get 
                Return _FirstName 
            End Get 
            Set(ByVal value As String) 
                _FirstName = value 
            End Set 
        End Property 
        Private _LastName As String 
        Public Property LastName() As String 
            Get 
                Return _LastName 
            End Get 
            Set(ByVal value As String) 
                _LastName = value 
            End Set 
        End Property 
    End Class
    Using this Class' .Serialize method:

    Code:
    Public Class Serialization 
        Public Function Serialize(ByVal FilePath As String, ByVal MyType As System.Type) As Boolean 
            Try 
                Using sw As New System.IO.StreamWriter(FilePath) 
                    Dim ser As New Xml.Serialization.XmlSerializer(MyType) 
                    ser.Serialize(sw, MyType) 
                End Using 
                Return True 
            Catch ex As Exception 
                Return False 
            End Try 
        End Function 
    End Class
    Calling it like this:

    Code:
    Public Class Form1 
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
            Dim ser As New Serialization 
            Dim p As New Person With {.FirstName = "MyFirstName", .LastName = "MyLastName"} 
            ser.Serialize("C:\test\person.xml", p.GetType) 
        End Sub 
    End Class
    But I get a "InvalidOperationException: There was an error generating the XML document" exception. I can't understand why the above does not work but if I change the .Serialize function to:

    Code:
    Public Class Serialization 
        Public Function Serialize(ByVal FilePath As String, ByVal MyType As Person) As Boolean 
            Try 
                Using sw As New System.IO.StreamWriter(FilePath) 
                    Dim ser As New Xml.Serialization.XmlSerializer(MyType.GetType) 
                    ser.Serialize(sw, MyType) 
                End Using 
                Return True 
            Catch ex As Exception 
                Return False 
            End Try 
        End Function 
    End Class
    It works...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trying to Serialize a class

    How can that first implementation possibly work? You're not passing the object you need to serialize into your Serialize method. You're passing in its type, not the object itself, so how can it ever be serialized?

    The second argument to XmlSerializer.Serialize is the object to serialize. If you want it to serialize a Person then you have to pas it a Person object, not a Type object that represents the Person class.

    Your Serialize method must take the object to serialize as an argument, not the type of the object. The type of the object will be determined from the object itself. Your method should look like this:
    vb.net Code:
    1. Public Class Serialization
    2.     Public Function Serialize(ByVal filePath As String, ByVal objectToSerialize As Object) As Boolean
    3.         Try
    4.             Using sw As New System.IO.StreamWriter(filePath)
    5.                 Dim ser As New Xml.Serialization.XmlSerializer(objectToSerialize.GetType())
    6.                 ser.Serialize(sw, objectToSerialize)
    7.             End Using
    8.             Return True
    9.         Catch ex As Exception
    10.             Return False
    11.         End Try
    12.     End Function
    13. End Class
    Does the renaming of the second parameter make it more obvious what's happening?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

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

    Re: Trying to Serialize a class

    Yes, wow I'm an idiot. Thanks. I'd rep but your turn has not come around yet!

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