< C++ & C# XML Serialization Program Examples | Main | Another C++ XML Serialization Program Example >

 


 

Chapter 4 Part 12:

Serialization

 

 

What do we have in this chapter 4 Part 12?

  1. VB .NET XML Serialization Program Example

 

 

VB .NET XML Serialization Program Example

 

Create a new class library project and you may want to use SimpleXmlSerializeVB name as the solution and project names.

 

VB .NET XML Serialization Program Example - creating a new class library project

 

Add the following Imports directives.

 

Imports System

Imports System.IO

Imports System.Xml.Serialization

Imports Microsoft.VisualBasic

 

Rename the class to Orchestra and add its code.

 

VB .NET XML Serialization Program Example - renaming the class to a new name

 

Add the following classes.

 

Imports System

Imports System.IO

Imports System.Xml.Serialization

Imports Microsoft.VisualBasic

 

Public Class Orchestra

    Public Instruments() As Instrument

End Class

 

Public Class Instrument

    Public Name As String

End Class

 

Public Class Brass

    Inherits Instrument

    Public IsValved As Boolean

End Class

 

Public Class Run

 

    Public Shared Sub Main()

        Dim test As New Run()

        Console.WriteLine("Calling the SerializeObject subroutine passing a file name...")

        test.SerializeObject("Override.xml")

        Console.WriteLine()

        Console.WriteLine("Calling the DeserializeObject subroutine passing a file name...")

        test.DeserializeObject("Override.xml")

    End Sub

 

    Public Sub SerializeObject(ByVal filename As String)

        ' Each overridden field, property, or type requires an XmlAttributes object.

        Console.WriteLine("Instantiating XmlAttributes object...")

        Dim attrs As New XmlAttributes()

 

        ' Create an XmlElementAttribute to override the

        ' field that returns Instrument objects. The overridden field

        ' returns Brass objects instead.

        Console.WriteLine("Creating an XmlElementAttribute to override the field that returns Instrument objects...")

        Console.WriteLine(" The overridden field returns Brass objects instead...")

        Dim attr As New XmlElementAttribute()

        attr.ElementName = "Brass"

        attr.Type = GetType(Brass)

        Console.WriteLine("Element name: " + attr.ElementName)

 

        ' Add the element to the collection of elements.

        Console.WriteLine("Adding the element to the collection of elements...")

        attrs.XmlElements.Add(attr)

 

        ' Create the XmlAttributeOverrides object.

        Console.WriteLine("Creating the XmlAttributeOverrides object...")

        Dim attrOverrides As New XmlAttributeOverrides()

 

        ' Add the type of the class that contains the overridden

        ' member and the XmlAttributes to override it with to the XmlAttributeOverrides object.

        Console.WriteLine("Adding the type of the class that contains the overridden...")

        Console.WriteLine("  member and the XmlAttributes to override it with, to the XmlAttributeOverrides object...")

        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)

 

        ' Create the XmlSerializer using the XmlAttributeOverrides.

        Console.WriteLine("Creating the XmlSerializer using the XmlAttributeOverrides...")

        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)

 

        ' Writing the file requires a TextWriter.

        Console.WriteLine("Writing the file using a StreamWriter...")

        Dim writer As New StreamWriter(filename)

 

        ' Create the object that will be serialized.

        Console.WriteLine("Creating the Orchestra object that will be serialized...")

        Dim band As New Orchestra()

 

        ' Create an object of the derived type.

        Console.WriteLine("Creating an object of the derived type...")

        Dim i As New Brass()

        i.Name = "Trumpet"

        i.IsValved = True

        Console.WriteLine("Element's name: " + i.Name)

        Dim myInstruments() As Instrument = {i}

        band.Instruments = myInstruments

 

        ' Serialize the object.

        Console.WriteLine("Serializing the object...")

        s.Serialize(writer, band)

        Console.WriteLine("Closing the StreamWriter...")

        writer.Close()

    End Sub

 

    Public Sub DeserializeObject(ByVal filename As String)

        Dim attrOverrides As New XmlAttributeOverrides()

        Dim attrs As New XmlAttributes()

 

        ' Create an XmlElementAttribute to override the Instrument.

        Console.WriteLine("Creating an XmlElementAttribute to override the Instrument...")

        Dim attr As New XmlElementAttribute()

        attr.ElementName = "Brass"

        attr.Type = GetType(Brass)

 

        ' Add the element to the collection of elements.

        Console.WriteLine("Adding the element to the collection of elements...")

        attrs.XmlElements.Add(attr)

 

        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)

 

        ' Create the XmlSerializer using the XmlAttributeOverrides.

        Console.WriteLine("Creating the XmlSerializer using the XmlAttributeOverrides...")

        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)

 

        Console.WriteLine("Creating a file stream...")

        Dim fs As New FileStream(filename, FileMode.Open)

        Console.WriteLine("Deserializing the file object...")

        Dim band As Orchestra = CType(s.Deserialize(fs), Orchestra)

        Console.WriteLine()

        Console.WriteLine("Brass:")

 

        ' The difference between deserializing the overridden

        ' XML document and serializing it is this: To read the derived

        ' object values, you must declare an object of the derived type

        ' (Brass), and cast the Instrument instance to it.

        Dim b As Brass

        Dim i As Instrument

        For Each i In band.Instruments

            b = CType(i, Brass)

            Console.WriteLine("Name: " + b.Name + ControlChars.Cr + "IsValved?: " + b.IsValved.ToString())

        Next i

    End Sub

End Class

 

Change the DLL to console application type project in order to test this program. The steps are shown below.

 

VB .NET XML Serialization Program Example - invoking the project properties page  

 

VB .NET XML Serialization Program Example - changing the project type from DLL to EXE and setting the startup to Main() subroutine

 

Build and run the project. The following is a sample output.

 

VB .NET XML Serialization Program Example - running the project with default argument values

 

The following is the XML file (Override.xml) generated and used for the serialization and de-serialization in this project

 

VB .NET XML Serialization Program Example - the generated XML file used for serialization and de-serialization opened in the Internet Browser

 

The following program examples provide code that performs XML serialization on a variety of classes: a simple class, one with XML attributes applied, and one that overrides serialization. See the sample for the complete code listing.

 

 

 


 

< C++ & C# XML Serialization Program Examples | Main | Another C++ XML Serialization Program Example >