What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleControl serialization and deserialization with attributes in Visual Basic .NET
DescriptionThis example shows how to control serialization and deserialization with attributes in Visual Basic .NET.
Keywordssetrialize, deserialize, serialization, XmlSerializer, attributes, VB.NET
CategoriesSoftware Engineering
For information on basic serialization and deserialization, see Serialize and deserialize objects in Visual Basic .NET.

By placing attributes on a class's properties, you can control how those classes are serialized. The following code shows a Person class that demonstrates several XML attributes. You might want to refer to the results at the end to see how each attribute affects the serialization.

  • XmlAttributeAttribute - This tells the serializer to make a property an attribute of the class rather than a separate element. It also gives the name that the attribute should have in the resulting XML. In this example, the FirstName property is saved as the GivenName attribute and the LastName property is saved as the FamilyName attribute.
  • XmlElement - This explicitly says that a property should be serialized as an element (the default) and can give the element a new name. In this example, the Street property is saved with the element name StreetAddress.
  • XmlEnum - Enumerated values are saved as textual representations. The XmlEnum attributes changes the name used for an enumerated value. For example, this program's code uses the enumerated ContactMethods values Post, Phone, and Email. The XML serialization calls those values SnailMail, Telephone, and Email.
  • XmlIgnore - This tells the serializer to not serialize or deserialize a property. This is useful for values that are created on the fly (such as the time or date the code called an object method), for properties that are duplicates of other properties (if the object has temperature properties for both Centigrade and Fahrenheit, you don't need to store both), and for properties that are other objects that are not serializable.
  • XmlArray - This tells the serializer what name to give an array property.
  • XmlArrayItem - This tells the serializer what name to give a value inside an array property. If you don't include this, the items are named after their data types. In this example, the items in the EmailAddresses array are named "string."
Imports System.Xml.Serialization

<Serializable()> _
Public Class Person
    <XmlAttributeAttribute("GivenName")> _
    Public FirstName As String

    <XmlAttributeAttribute("FamilyName")> _
    Public LastName As String

    <XmlElement("StreetAddress")> _
    Public Street As String

    Public Enum ContactMethods
        <XmlEnum("SnailMail")> _
        <XmlEnum("Telephone")> _
        <XmlEnum("Email")> _
    End Enum
    <XmlElement("ContactMethod")> _
    Public PreferredMethod As ContactMethods

    Public City As String
    Public State As String

    <XmlIgnore()> _
    Public Zip As String

    <XmlArray("PhoneNumbers"), XmlArrayItem("PhoneNum")> _
    Public Phones(1) As String

    <XmlArray("EmailAddresses")> _
    Public Emails(1) As String

    ' Empty constructor required for serialization.
    Public Sub New()
    End Sub

    Public Sub New(ByVal new_FirstName As String, ByVal _
        new_LastName As String, ByVal new_Street As String, _
        ByVal new_City As String, ByVal new_State As _
        String, ByVal new_Zip As String, ByVal phone1 As _
        String, ByVal phone2 As String, ByVal email1 As _
        String, ByVal email2 As String, ByVal _
        contact_method As ContactMethods)
        FirstName = new_FirstName
        LastName = new_LastName
        Street = new_Street
        City = new_City
        State = new_State
        Zip = new_Zip
        Phones = New String() {phone1, phone2}
        Emails = New String() {email1, email2}
        PreferredMethod = contact_method
    End Sub
End Class
The following text shows the result. Notice that the Zip property, marked with XmlIgnore, is nowhere to be found.
<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema"
GivenName="Mickey" FamilyName="Moose">
  <StreetAddress>1337 Leet St</StreetAddress>
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.