Listing 12.13 shows a class that describes books. The Book class is quite trivial, except that each book can have any number of authors. The authors are stored in an array of Book.Author objects.
Listing 12.13: Book Class
Public Class Book Private _title As String Private _pages As Integer Private _price As Decimal Private _authors() As Author Public Sub New() End Sub Public Property Title() As String Get Return _title End Get Set(ByVal Value As String) If Value.Length > 100 Then _title = Value.Substring(0, 99) Else _title = Value End If End Set End Property Public Property Pages() As Integer Get Return _pages End Get Set(ByVal Value As Integer) _pages = Value End Set End Property Public Property Price() As Decimal Get Return _price End Get Set(ByVal Value As Decimal) _price = Value End Set End Property Public Property Authors() As Author() Get Return (_authors) End Get Set(ByVal Value As Author()) _authors = Value End Set End Property Public Class Author Private _firstname As String Private _lastname As String Public Property FirstName() As String Get Return _firstname End Get Set(ByVal Value As String) If Value.Length > 50 Then _firstname = Value.Substring(0, 49) Else _firstname = Value End If End Set End Property Public Property LastName() As String Get Return _lastname End Get Set(ByVal Value As String) If Value.Length > 50 Then _lastname = Value.Substring(0, 49) Else _lastname = Value End If End Set End Property End Class End Class
The following statements create a new Book object, which includes three authors:
Code language: PHP (php)
Private BK0 As New Book Dim authors(2) As Book.Author authors(0) = New Book.Author authors(0).FirstName = "Author1 First" authors(0).LastName = "Author1 Last" authors(1) = New Book.Author authors(1).FirstName = "Author2 First" authors(1).LastName = "Author2 Last" authors(2) = New Book.Author authors(2).FirstName = "Author3 First" authors(2).LastName = "Author3 Last" BK0.Title = "Book Title" BK0.Pages = 234 BK0.Price = 29.95 BK0.Authors = authors
Let’s see how to serialize arrays of objects in XML format, starting with a hard rule: The array to be serialized must be typed. All the elements of the array should have the same type, which must match the type you pass to the constructor of the XmlSerializer class. You can’t create an array of objects and store objects of different types to it. No warning will be issued at design time, but a runtime exception will be thrown as soon as your code reaches the Serialize method. The XmlSerializer is constructed for a specific type of object, and any given instance of this class can handle objects of the specific type and nothing else.
To serialize the Book objects created with the preceding statements, we’ll first create an array of the Book type and store a few properly initialized instances of the Book class to its elements. Then we’ll pass this array to the Serialize method of the XmlSerializer class, as shown in Listing 12.14.
Listing 12.14: XML Serialization of an Array of Objects
Code language: PHP (php)
Private Sub bttnSaveArrayXML Click(...)Handles bttnSaveArrayXML.Click Me.Cursor = Cursors.WaitCursor Dim AllBooks(3) As Book AllBooks(0) = BK0 AllBooks(1) = BK1 AllBooks(2) = BK2 AllBooks(3) = BK3 Dim serializer As New XmlSerializer(AllBooks.GetType) Dim FS As FileStream Try FS = New FileStream("..\SerializedXMLArray.xml", _ FileMode.Create) serializer.Serialize(FS, AllBooks) Catch exc As Exception MsgBox(exc.InnerException.ToString) Exit Sub Finally FS.Close() End Try Me.Cursor = Cursors.Default bttnLoadArrayXML.Enabled = True TextBox1.Clear() TextBox1.Text = _ "Array of Book objects saved in file SerializedXMLArray.xml" End Sub
The XmlSerializer class’s constructor accepts as an argument the array type. Because the array is typed, it can figure out the type of custom objects it will serialize.
There’s a substantial overhead the first time you create an instance of the XmlSerializer class, but the process isn’t repeated during the course of the application. There is overhead because the CLR creates a temporary assembly for serializing and deserializing the specific type. This assembly, however, remains in memory for the course of the application, and the initial overhead won’t recur. This means that although there will be an additional delay of a couple of seconds when the application starts (or whenever you load the settings), you can persist the class with the application’s configuration every time the user changes one of the settings without any performance penalty.