0% found this document useful (0 votes)
7 views

complete-reference-vb_net_74

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

complete-reference-vb_net_74

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Serialization with XML

Let's make the linked list class we constructed in Chapter 13 work with serialization. This will allow a
consumer of the linked list class not only to persist the data while an application works on the list, but every
time the application starts up, the object and data can be automatically reconstituted inside the application
ready for use.

Before we can use the serialization classes, we have to decorate the classes we want to serialize as being
serializable. Let's apply this to our linked list class (BaseNodeContainer). This entails providing serialization
attributes for the base class. This is done with the following code, part of which has been set in bold, so that
you can easily see the changes from the class constructed earlier:

Option Explicit On
Option Strict On

Imports System
Imports System.Runtime.Serialization

<Serializable()> Public Class BaseNodeCollection

What's now different about the preceding class compared to the classes shown throughout the book? For
starters, the class makes a reference to the Serialization classes by importing the
System.Runtime.Serialization namespace. This is defined using the Visual Basic .NET attributes indicators,
<>.

But remember that the BaseNodeContainer class also contains a composite Node class so we need to
decorate that class with the same attribute as follows:

<Serializable()> Public Class Node

You next need to provide a method in the base class that performs the actual serialization. But before you can
do that, you need to build the file and stream support into the class to get the data in the object to the file
system, and store it in a folder somewhere. This can now be easily accomplished with the extensive I/O
facilities we have covered in earlier parts of this chapter, which by now we should have mastered.

To do the object serializing and persist the data, you will need to create a method that walks the graph,
serializes the data in the SOAP/XML format, and then saves the stream into a file. The following method
achieves that objective:

Public Sub PlayOutList(ByVal target As String)


Dim meData As New SoapFormatter()
Try
If File.Exists(target) Then
File.Delete(target)
ElseIf (Not File.Exists(target)) Then
Dim fileForObject As New FileStream(target, IO.FileMode.Create)
meData.Serialize(fileForObject, Me)
fileForObject.Close()
End If
Catch e As Exception
Me.exceptinfo = e.Message
End Try
End Sub

The first step in actually implementing serialization using the SOAP format is to create a new SoapFormatter
object to process the SOAP stream, and this was done using the following line:

558

You might also like