To Serialize or Not to Serialize?

J

Joe Cool

I have been using the Help topics on Serialize and Deserialize to
figure out how they work. I keyed in the VB samples and the Serialize
on works just fine. But if you try to feed the output XML file created
with the Serialize sample to the Deserialize sample, it does not work.
But if I manually edit the simple.xml file and make a few changes the
Deserialize sample now works. Just what is causing the first attempt
on the more complex XML file to fail?

Original XML file created with Serialize sample code (newlines have
been added)

<?xml version="1.0" encoding="utf-8"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com"
mlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23.0</money:LineTotal>
</OrderedItem>

When you run the Deserialize sample on this file your output is:
Reading with XmlReader
0 0 0

But if I change the XML file slightly to this:

<OrderedItem>
<ItemName>Widget</ItemName>
<Description>Regular Widget</Description>
<UnitPrice>2.3</UnitPrice>
<Quantity>10</Quantity>
<LineTotal>23.0</LineTotal>
</OrderedItem>

the output from the Deserialize sample is:

Reading with XmlReader
Widget Regular Widget 2.3 10 23.0

How come?

Here is the Deserialize code:

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

Public Class OrderedItem

<XmlElement(Namespace:="http://www.cpandl.com")> _
Public ItemName As String

<XmlElement(Namespace:="http://www.cpandl.com")> _
Public Description As String

<XmlElement(Namespace:="http://www.cohowinery.com")> _
Public UnitPrice As Decimal

<XmlElement(Namespace:="http://www.cpandl.com")> _
Public Quantity As Integer

<XmlElement(Namespace:="http://www.cohowinery.com")> _
Public LineTotal As Decimal

' a custom method to calculate price per item
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class

Public Class Test

Public Shared Sub Main()

Dim t As New Test

' write a new purchase order
t.SerializeObject("simple.xml")

End Sub

Private Sub SerializeObject(ByVal filename As String)

Console.WriteLine("Writing With XmlTextWriter")

Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With

' create as xmlserializernamespaces object
Dim ns As New XmlSerializerNamespaces
' add two namespaces with prefixes
ns.Add("inventory", "http://www.cpandl.com")
ns.Add("money", "http://www.cohowinery.com")
' create an xmltextwriter using filestream
Dim fs As New FileStream(filename, FileMode.Create)
Dim writer As New XmlTextWriter(fs, New UTF8Encoding)
' serialize using the xmltextwriter
serializer.Serialize(writer, i, ns)
writer.Close()
End Sub
End Class
 
L

Landley

I always avoid serialization of classes. The class is not going change, it
is fine, but if you add properties etc, you cannot deserialize the XML or
binary that was created with the previous version of the class. If the
class is permanent, i.e. will never change interface, this method is fine.

L.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top