Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Gurus*
Posted

Say you have a class named myDog. You take the class myDog, along with all its properties, such as size and color, and persist it in a format that can be saved to a file or sent over a network. It's basically like taking a live dog, and saving it to a file. Of course you can also deserialize what's been serialized, essentially taking the file and turning it back into the class myDog. If you're further interested in the topic, you'll discover both binary and XML serialization. Binary serialization can save an object to memory, save it to disk or send it over a network. XML serialization saves the object in a format that is human readable, such as the following:

<dog>
   <color>black</color>
   <size>4</size>
</dog>

It's a fairly useful technology. Learn more about it in MSDN:

http://ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpovrserializingobjects.htm

  • *Gurus*
Posted
I find it useful for file formats. Where before you might invent a file format for your program, read it line by line and parse it to create your class, now you just use a couple of lines to serialize an instance of the class to disk in XML format (or other).

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • *Experts*
Posted

Derek, I didn't know dogs had sizes... or is that supposed to be

its shoe size? :D

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted
So it's like golf... the smaller the number, the better.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • Leaders
Posted

Speaking of serialization, I'm having trouble serializing collections. I've got a custom object that inherits an arraylist which is filled with custom objects, I can serialize the single object fine but the custom collection gives me the following exception. Any ideas?

 

An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll

 

Additional information: There was an error generating the XML document.

--tim
  • Leaders
Posted

Yep, I think I'm doing what I've read, and dumbed it down to be a fairly simple test. It has problems with any of my custom objects being in the arraylist.

Public Class Form1
   Inherits System.Windows.Forms.Form


   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim cfl As New ArrayList() 

       cfl.Add(New SimpleDate())


       cfl.TrimToSize()

       Dim ser As XmlSerializer = New XmlSerializer(GetType(ArrayList))
       Dim writer As TextWriter = New StreamWriter("adminSettings.xml")
       ser.Serialize(writer, cfl)
       writer.Close()

   End Sub
End Class


<Serializable()>Public Class SimpleDate
   Public SimpleDate As DateTime
End Class


--tim
Posted

Thanks for the explanation. I think I understand the concept.

 

What is the purpose of the <serializable> tag I sometimes see on functions and subs?

 

p.s. I couldn't get that link to work

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...