Site Map       
Home
Web Services
XML in .NET
XML Schema
XSLT and XPath
XML Serialization
Database and XML
XML Query
ASP.NET
Contact Info

.NET Notes

 

Using Arrays With XML Serialization

XML Serialization can be used with objects that contain arrays. This example uses a product class where the product can come in several colors.

               



        
'Product Class

Public Class ProductB

    Private ProdId As Integer
    Private ProdName As String
    Private ProdPrice As Decimal
    Private ProdColors As String()

    Public Property ProductColors() As String()
        Get
            Return ProdColors
        End Get
        Set(ByVal Value As String())
            ProdColors = Value
        End Set
    End Property

    Public Property ProductID() As Integer
        Get
            Return ProdId
        End Get
        Set(ByVal Value As Integer)
            ProdId = Value
        End Set
    End Property

    Public Property ProductName() As String
        Get
            Return ProdName
        End Get
        Set(ByVal Value As String)
            ProdName = Value
        End Set
    End Property

    Public Property ProductPrice() As Decimal
        Get
            Return ProdPrice
        End Get
        Set(ByVal Value As Decimal)
            ProdPrice = Value
        End Set
    End Property

End Class

'Sample Code

        Dim prd As New ProductB()
        Dim sw As New StringWriter()
        Dim ser As New XmlSerializer(GetType(ProductB))

        Try
            prd.ProductID = 1724
            prd.ProductName = "Flyswatter"
            prd.ProductPrice = 3.95
            prd.ProductColors = New String() {"white", "black", "red", "blue"}

            ser.Serialize(sw, prd)

        Catch
            'Handle Errors

        Finally
            'Clean Up
        End Try
    

Copyright © 2015 Seanco Technology, Inc.  All rights reserved.