XmlArrayItemAttribute Constructor
Collapse the table of content
Expand the table of content

XmlArrayItemAttribute Constructor ()


Initializes a new instance of the XmlArrayItemAttribute class.

Namespace:   System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

Public Sub New

The following example serializes a class named Transportation that contains a field named MyVehicles that returns an array of Vehicle objects. The example applies the XmlArrayItemAttribute to the field, allowing the XmlSerializer to insert instances of the Car class, which is derived from the Vehicle class, into the array.

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Class Vehicle
    Public id As String
End Class

Public Class Car
    Inherits Vehicle
    Public Maker As String
End Class

Public Class Transportation
    <XmlArrayItem(), _
     XmlArrayItem(GetType(Car), ElementName := "Automobile")> _
    Public MyVehicles() As Vehicle
End Class

Public Class Run

    Public Shared Sub Main()
        Dim test As New Run()
    End Sub

    Private Sub SerializeObject(ByVal filename As String)
        ' Creates an XmlSerializer for the Transportation class. 
        Dim MySerializer As New XmlSerializer(GetType(Transportation))

        ' Writing the XML file to disk requires a TextWriter.
        Dim myTextWriter As New StreamWriter(filename)

        ' Creates the object to serialize.
        Dim myTransportation As New Transportation()

        ' Creates objects to add to the array.
        Dim myVehicle As New Vehicle()
        myVehicle.id = "A12345"

        Dim myCar As New Car()
        myCar.id = "Car 34"
        myCar.Maker = "FamousCarMaker"

        myTransportation.MyVehicles = New Vehicle() {myVehicle, myCar}

        ' Serializes the object, and closes the StreamWriter.
        MySerializer.Serialize(myTextWriter, myTransportation)
    End Sub

    Private Sub DeserializeObject(ByVal filename As String)
        ' Create an XmlSerializer instance.
        Dim mySerializer As New XmlSerializer(GetType(Transportation))
        Dim myFileStream As New FileStream(filename, FileMode.Open)
        Dim myTransportation As Transportation = _
            CType(mySerializer.Deserialize(myFileStream), Transportation)

        Dim i As Integer
        For i = 0 To myTransportation.MyVehicles.Length - 1
        Next i
    End Sub
End Class

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2016 Microsoft