How to serialize an object to XML by using Visual Basic .NET or Visual Basic 2005

Article translations Article translations
Article ID: 315703 - View products that this article applies to.
This article was previously published under Q315703
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to serialize an object to XML by using Visual Basic .NET or Visual Basic 2005. This method is useful for persisting an object's state. This method is also useful for cloning an object by de-serializing the XML back to a new object.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
This article assumes that you are familiar with the following topics:
  • General familiarity with XML
  • General familiarity with Visual Basic .NET or Visual Basic 2005

XML Serialization

Serialization is the process of taking the state of an object and persisting it in some fashion. The Microsoft .NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this capability.

Follow these steps to create a console application that creates an object and then serializes its state to XML:
  1. Create a new console application in Visual Basic .NET or in Visual Basic 2005.
  2. Add a new class to the project by clicking Add Class on the Project menu.
  3. In the Add New Item dialog box, change the name of the class to clsPerson.
  4. Click Open. A new class is created.

    Note In Visual Studio 2005, click Add.
  5. Add the following code after the Public Class clsPerson statement:
      Public FirstName As String
      Public MI As String
      Public LastName As String
    					
    This creates three properties for the class. These properties are implemented as class-level Public variables, but this technique also works well with Property Get/Set procedures.

  6. Switch to the code window for Module1.vb.
  7. In the Sub Main() procedure, declare and create an instance of the clsPerson class:
      Dim p As New clsPerson()
    					
  8. Set the clsPerson object's properties:
        p.FirstName = "Jeff"
        p.MI = "A"
        p.LastName = "Price"
    					
  9. The Xml.Serialization namespace contains an XmlSerializer class that serializes an object to XML. When you create an instance of XmlSerializer, you pass the type of the class that you want to serialize into its constructor:
    Dim x As New Xml.Serialization.XmlSerializer(p.GetType)
    					
  10. The Serialize method is used to serialize an object to XML. Serialize is overloaded and can send output to a TextWriter, Stream, or XMLWriter object. In this example, you send the output to the console:
    x.Serialize(Console.Out, p)
    Console.WriteLine()
    Console.ReadLine()
    					

Complete Code Listing

Public Class clsPerson
  Public FirstName As String
  Public MI As String
  Public LastName As String
End Class

Module Module1
  Sub Main()
    Dim p As New clsPerson()
    p.FirstName = "Jeff"
    p.MI = "A"
    p.LastName = "Price"

    Dim x As New Xml.Serialization.XmlSerializer(p.GetType)
    x.Serialize(Console.Out, p)
    Console.WriteLine()
    Console.ReadLine()
  End Sub
End Module
				

Verification

To verify that your project works, press CTRL+F5 to run the project. A clsPerson object is created and populated with the values that you entered; this state is serialized to XML. The console window shows:
<?xml version="1.0" encoding="IBM437"?>
<clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Jeff</FirstName>
  <MI>A</MI>
  <LastName>Price</LastName>
</clsPerson>
					

Troubleshooting

The Xml.Serialization.XmlSerializer object performs only shallow serialization. If you also want to serialize an object's private variables or child objects, you must use deep serialization.

REFERENCES

For more information, visit the following MSDN Web site:
Introducing XML Serialization

Properties

Article ID: 315703 - Last Review: December 6, 2006 - Revision: 2.3
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbhowtomaster KB315703

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com