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

 

XML Object Serialization

The following example shows how XML Serialization can be applied to a class that represents a consumer product.

               


        
'Product Class

    Public Class Product

    Private ProdId As Integer
    Private ProdName As String
    Private ProdPrice As Decimal

    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 XML Object Serialization Code

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

        Try
            prd.ProductID = 1724
            prd.ProductName = "Flyswatter"
            prd.ProductPrice = 3.95

            ser.Serialize(sw, prd)

        Catch
            'Handle Errors

        Finally
            'Clean Up
        End Try
    

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