ISerialization Frustration !

G

Guest

I'm not sure if I'm having a moment of non-clarity or not but I don't believe
implementing ISerialization works at all.

It started when I had a class that I wanted to provide my own serialization
for because it had ReadOnly properties. So I implemented the ISerializable
interface.
I through a breakpoint into the GetObjectData() method and the New(info,
context) constructor and the breakpoint was not hit.

At this point, I realized that the automatic serialization was being
performed and not the ISerialization interface. Here's sample code:

' This is the class that supposed to be "ISerializable"

Imports System.Runtime.Serialization
Imports System.Xml.Serialization

<Serializable()> _
Public Class SerializableObject
Implements ISerializable

Private _TestData As String
Private _HiddenData As String

Public Property TestData() As String
Get
Return _TestData
End Get
Set(ByVal value As String)
_TestData = value
End Set
End Property

Public Sub New()

End Sub

Protected Sub New(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext)
'Try putting a breakpoint here, it won't be hit.
_TestData = info.GetString("TestData")
_HiddenData = info.GetString("HiddenData")
End Sub

Public Sub GetObjectData(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext) Implements
System.Runtime.Serialization.ISerializable.GetObjectData
'Try putting a breakpoint here, it won't be hit.
info.AddValue("TestData", _TestData, GetType(String))
info.AddValue("HiddenData", "hidden", GetType(String))
End Sub
End Class


Here's some code to test the serialization in the Form_Load event so you
don't have to click anything to test it:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim s As New XmlSerializer(GetType(SerializableObject))
Dim o As New SerializableObject
o.TestData = "Test"

'Serialize first.
Dim sb As New System.Text.StringBuilder
Dim sw As New StringWriter(sb)
s.Serialize(sw, o)
sw.Close()
'Clear the object to make sure there's nothing up my sleeve.
o = Nothing
'Deserialize the object.
Dim sr As New StringReader(sb.ToString)
o = s.Deserialize(sr)
TextBox1.Text = o.TestData

'Take a look at the XML generated. You'll see that the property
called "Hidden" isn't there.
'That means that custom serialization didn't run.
MessageBox.Show(sb.ToString)

End Sub


If ANYONE knows what's going on and how I can get ISerialization to work for
me, please let me know. I've tried all the MSDN and google links to no avail.
Thanks.
 
L

Lloyd Dupont

Haheum....

I don't know what is this 'ISerialization' you are talking about but let's
say the ISerializable is meant to be used with the BinaryFormate.

So I would say: Get rid if this XmlSerializer.

But I guess the issue is more like you want to use the XmlSerializer, so I
will recommend you read its documentation.
They mention things like XmlElementAttribute in it (and other things) which
you might find helpfull!

RTFM man!
 
G

Guest

Actually, it is "ISerializable" that I've implemented (what is ISerialization
anyway?)
But you're right, I suppose it should be IXMLSerializabble - NO THANKS TO
THE MSDN LIBRARY!. It says that IXMLSerializable is to be used internally
(etc..., etc..., etc...)
 
L

Lloyd Dupont

Actually, it is "ISerializable" that I've implemented (what is
ISerialization
anyway?)
I don't know! It's just the title of your post, so you tell me!...
But you're right, I suppose it should be IXMLSerializabble - NO THANKS TO
THE MSDN LIBRARY!. It says that IXMLSerializable is to be used internally
(etc..., etc..., etc...)
ungratefull one... tssk...
 
J

Jeff Gaines

'Take a look at the XML generated. You'll see that the property
called "Hidden" isn't there.
'That means that custom serialization didn't run.
MessageBox.Show(sb.ToString)

End Sub


If ANYONE knows what's going on and how I can get ISerialization to work
for
me, please let me know. I've tried all the MSDN and google links to no
avail.
Thanks.

Doesn't Serialisation only serialise public properties/variables? I'm sure
when I was looking at it once I got an error that there were no public
(something, can't remember) to serialise.
 
G

Guest

Ungrateful? No, not at all. Thank you (which I stated in advance in the
otiginal post) My dismay is with the MSDN library for it's lack of
documentation on IXMLSerializable.
 
G

Guest

Jeff - it will supposedly automatically serialize public properties unless
you implement ISerializable and serialize/deserialize only the data you
choose.
 
G

Guest

Following up on my original post:

The reason it was not executing my ISerializable code was because I
should've been implementing IXMLSerializable which I ofcourse didn't find out
in the MSDN. Actually MSDN states that IXMLSerializable is not for public use
and the documentation ended there (atleast in MSDN 01-2006). BAD MSDN! BAD
BAD MSDN!
 
L

Lloyd Dupont

Ok, misunderstanding.. ;-)

I will add that I'm not uing MSDN but the Help application which comes with VS.NET / .NET SDK.
In the index tab, in the 'look for' text box you could type: 'XML serialization'
plenty of interesting reading there.

You will probably be interested in the subsection: "Attributes That Control XML Serialization"

Let me copy paste the array on this page:


Attribute Applies to Specifies
XmlAnyAttributeAttribute
Public field, property, parameter, or return value that returns an array of XmlAttribute objects.
When deserializing, the array will be filled with XmlAttribute objects that represent all XML attributes unknown to the schema.

XmlAnyElementAttribute
Public field, property, parameter, or return value that returns an array of XmlElement objects.
When deserializing, the array is filled with XmlElement objects that represent all XML elements unknown to the schema.

XmlArrayAttribute
Public field, property, parameter, or return value that returns an array of complex objects.
The members of the array will be generated as members of an XML array.

XmlArrayItemAttribute
Public field, property, parameter, or return value that returns an array of complex objects.
The derived types that can be inserted into an array. Usually applied in conjunction with an XmlArrayAttribute.

XmlAttributeAttribute
Public field, property, parameter, or return value.
The member will be serialized as an XML attribute.

XmlChoiceIdentifierAttribute
Public field, property, parameter, or return value.
The member can be further disambiguated by using an enumeration.

XmlElementAttribute
Public field, property, parameter, or return value.
The field or property will be serialized as an XML element.

XmlEnumAttribute
Public field that is an enumeration identifier.
The element name of an enumeration member.

XmlIgnoreAttribute
Public properties and fields.
The property or field should be ignored when the containing class is serialized.

XmlIncludeAttribute
Public derived class declarations, and return values of public methods for Web Services Description Language (WSDL) documents.
The class should be included when generating schemas (to be recognized when serialized).

XmlRootAttribute
Public class declarations.
Controls XML serialization of the attribute target as an XML root element. Use the attribute to further specify the namespace and element name.

XmlTextAttribute
Public properties and fields.
The property or field should be serialized as XML text.

XmlTypeAttribute
Public class declarations.
The name and namespace of the XML type.
 

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