Object Serialization

  • Thread starter Rodrigo Defavari
  • Start date
R

Rodrigo Defavari

Hello People !!

I'm having to work with serialization of objects (to transform the
properties in XML), but I am with the following problem. . .

It is only possible make serialization in objects marked as
"serializable ", and I am working with objects that inherit of
System.Windows.forms that are not marked as serializable. Example: Button,
Textbox, CheckBox.

To exemplify better: I am needing to save in a XML file all the
properties of a control as button, textbox, checkbox, etc..

Does anybody have some idea?


Thanks a lot !!!

--



Rodrigo Defavari
Developer
(e-mail address removed)
 
K

Kerry Moorman

Rodrigo,

It sounds like you are trying to use xml serialization. If that is the case,
xml serialization does not require the object to be serialized to be marked
as serializable.

Kerry Moorman
 
P

Phill W.

Rodrigo said:
To exemplify better: I am needing to save in a XML file all the
properties of a control as button, textbox, checkbox, etc..

Serialisation will save /every/ property on the control. Think about
just how many properties, say, a TextBox has and you'll be looking at a
/lot/ of data.

Question: do you need /all/ of these values?

Look at the properties that you're actually /changing/ in your
application and add a method that will allow you to save /just/ those.
Then add another method to read this data back and configure an
equivalent control using the same parameters.

Interface IPortable
Sub SaveTo( ele as XmlElement )
Sub ReloadFrom( ele as XmlElement )
End Interface

Class PortableTextBox
Inherits TextBox
Implements IPortable

Private Sub SaveTo( ele as XmlElement ) _
Implements IPortable.SaveTo
' Add Xml elements/attributes
End Sub

Private Sub ReloadFrom( ele as XmlElement ) _
Implements IPortable.ReloadFrom
' Set properties from Xml elements/attributes
End Sub

End Class

Lastly, if this "serialised" data is being saved /anywhere/ outside your
program, make sure you include a "version number" in it somewhere, so
that the ReloadFrom() routine can cater for changes that you might make
over time.

HTH,
Phill W.
 
R

Rodrigo Defavari

Hello Phill W.

In my case, the user will have access the properties through component
PropertyGrid to personalize the controls and also to create new. For this
reason I wanted to save all the properties.

I didn't understand that part of the code that you passed me:

Private Sub SaveTo( ele as XmlElement ) _
Implements IPortable.SaveTo
' Add Xml elements/attributes
End Sub

What does the line ' Add Xml elements/attributes mean?


Sorry, my english is very bad!!! :(

Rodrigo Defavari
 
P

Phill W.

Rodrigo said:
In my case, the user will have access the properties through component
PropertyGrid to personalize the controls and also to create new. For this
reason I wanted to save all the properties.

Remember that if you're working with Inherited Controls, you can Shadow
properties and, at the same time, mark them as /not/ being "Browsable".
That way, they won't show up in the Properties Designer (well, not in
the IDE, anyway, I'm not sure about the PropertyGrid).
Private Sub SaveTo( ele as XmlElement ) _
Implements IPortable.SaveTo
' Add Xml elements/attributes
End Sub

What does the line ' Add Xml elements/attributes mean?

I'm assuming here that you already have an XmlDocument containing at
least one content XmlElement. This routine takes each Property of the
control and adds it to the Xml, something like:

Private Sub SaveTo( ele as XmlElement ) _
Implements IPortable.SaveTo

' Add Xml elements/attributes
Dim node as XmlElement = ele.ownerDocument.CreateElement("control")

node.SetAttribute( "name", Me.Name )
node.SetAttribute( "type", "TextBox" )
node.SetAttribute( "left", Me.Left )
node.SetAttribute( "top", Me.Top )
node.SetAttribute( "width", Me.Width )
node.SetAttribute( "height", Me.Height )
node.SetAttribute( "text", Me.Text )
. . .

ele.AppendChild( node )

End Sub

HTH,
Phill W.
 

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

Top