Export (0) Print
Expand All

XmlAttributes.XmlElements Property

Gets a collection of objects that specify how the XmlSerializer serializes a public field or read/write property as an XML element.

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

'Declaration
Public ReadOnly Property XmlElements As XmlElementAttributes

Property Value

Type: System.Xml.Serialization.XmlElementAttributes
An XmlElementAttributes that contains a collection of XmlElementAttribute objects.

For each overridden member that is serialized as an XML element, you must add a new XmlElementAttribute to an XmlElementAttributes by calling the Add method. By default, an XmlElementAttributes object is created and assigned to the XmlElements property.

The following example serializes the Transportation class, which contains a single field named Vehicles that returns an ArrayList. The example applies two XmlElementAttribute attributes to the Vehicles field. The example creates two XmlElementAttribute objects and adds them to the XmlElementAttributes collection of an XmlAttributes object. To allow the array to accept different object types, the XmlAttributes object is added to the XmlAttributeOverrides object.

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


Public Class Transportation
    ' Subsequent code overrides these two XmlElementAttributes.
    <XmlElement(GetType(Car)), _
     XmlElement(GetType(Plane))> _
    Public Vehicles As ArrayList
End Class 

Public Class Car
    Public Name As String 
End Class 

Public Class Plane
    Public Name As String 
End Class 

Public Class Truck
    Public Name As String 
End Class 

Public Class Train
    Public Name As String 
End Class 


Public Class Test

    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("OverrideElement.xml")
    End Sub 


    ' Return an XmlSerializer used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributes and XmlAttributeOverrides objects. 
        Dim attrs As New XmlAttributes()

        Dim xOver As New XmlAttributeOverrides()


        ' Create an XmlElementAttribute to override 
        ' the Vehicles property.  
        Dim xElement1 As New XmlElementAttribute(GetType(Truck))
        ' Add the XmlElementAttribute to the collection.
        attrs.XmlElements.Add(xElement1)

        ' Create a second XmlElementAttribute, and 
        ' add it to the collection.  
        Dim xElement2 As New XmlElementAttribute(GetType(Train))
        attrs.XmlElements.Add(xElement2)

        ' Add the XmlAttributes to the XmlAttributeOverrides, 
        ' specifying the member to override. 
        xOver.Add(GetType(Transportation), "Vehicles", attrs)

        ' Create the XmlSerializer, and return it. 
        Dim xSer As New XmlSerializer(GetType(Transportation), xOver)
        Return xSer
    End Function 


    Public Sub SerializeObject(ByVal filename As String)
        ' Create an XmlSerializer instance. 
        Dim xSer As XmlSerializer = CreateOverrider()

        ' Create the object and serialize it. 
        Dim myTransportation As New Transportation()

        ' Create two new override objects that can be
'
        ' inserted into the array. 
        myTransportation.Vehicles = New ArrayList()
        Dim myTruck As New Truck()
        myTruck.Name = "MyTruck" 

        Dim myTrain As New Train()
        myTrain.Name = "MyTrain"

        myTransportation.Vehicles.Add(myTruck)
        myTransportation.Vehicles.Add(myTrain)

        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myTransportation)
    End Sub 
End Class

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft