Imports System.Xml.Serialization
Imports Microsoft.VisualBasic
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
'Create an instance of the XmlSerializer specifying type and namespace.
'the 'OrderedItem' class is below
Dim serializer As New XmlSerializer(GetType(OrderedItem))
'open an XmlReader from a string
'the getXMLDoc function returns an XML document in string format
Dim reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(getXMLDoc))
' Use the Deserialize method to restore the object's state.
i = CType(serializer.Deserialize(reader), OrderedItem)
' Write out the properties of the object.
Console.WriteLine("Item Name: " & i.ItemName)
Console.WriteLine("Item Desc: " & i.Description)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Function getXMLDoc() As String
Dim xElement As XElement
<ItemName>Widget</ItemName>
<Description>Regular Widget</Description>
<Quantity>10</Quantity>
<money:UnitPrice>2.3</money:UnitPrice>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
Return xElement.ToString
End Function
End Class
' This is the class that will be deserialized.
Public Class OrderedItem
<XmlElement()> _
Public ItemName As String
<XmlElement()> _
Public Description As String
<XmlElement()> _
Public Quantity As Integer
Public UnitPrice As Decimal
Public LineTotal As Decimal
' A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class