Re: To Serialize or Not to Serialize?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

"Joe Cool" <joecool@xxxxxxxx> wrote in message
news:8iual1h984h5ojm8760vhon57qgjncag8g@xxxxxxxxxx
> 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
>
>


.



Relevant Pages

  • XML Serialization : Error reflecting field....
    ... I have to deserialize an XML document to objects and then serialize it back to XML to pass to the stored proc. ... Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching. ...
    (microsoft.public.dotnet.xml)
  • XML Serialization : Error reflecting field....
    ... I have to deserialize an XML document to objects and then serialize it back to XML to pass to the stored proc. ... "There was an error reflecting field 'ords'." ...
    (microsoft.public.dotnet.xml)
  • serialization without xml format?
    ... A co-worker recently insisted that you cannot serialize and deserialize ... have the ability to serialize and deserialize themselves to a text file of a ... proprietary format without using XML? ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: how to eval a statement from an xml file, in C#?
    ... create the xml that represents a class in xml ... just deserialize it for use in the codebehind when needed ... I mean if I have a regular xml file out there, ... > serialize and grab in .NET or does it need certain header info etc. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: version compatibility when serializing and deserializing
    ... to deserialize an object that lacks that member, ... file with the serialized object and I didn't get any exception. ... You would need to serialize an ...
    (microsoft.public.dotnet.languages.csharp)