I have an object called Place which contains only string properties and has
the <Serializable()> flag before the class name declaration. I also have a
collection object called Places, which is implemented using Inherits
System.Collections.Specialized.NameObjectCollectio nBase and also has the
<Serializable()> flag before the class name declaration.
In the calling code, I'm successfully serializing the object to an XML file
using a SoapFormatter object and a FileStream object. However, I am running
into a problem in deserializing the data back into the Places object. The
deserialize method returns the error: "The constructor to deserialize an
object of type TestingPad.Places was not found." Any ideas what might cause
the Deserialize method to return this error while the Serialize method works
flawlessly? Below is the code I'm using:
<Serializable()> Public Class Place
Private strCity As String
Private strState As String
Public Sub New()
strCity = ""
strState = ""
End Sub
Public Sub New(ByVal City As String, ByVal State As String)
strCity = City
strState = State
End Sub
Public Property City() As String
Get
Return strCity
End Get
Set(ByVal Value As String)
strCity = Value
End Set
End Property
Public Property State() As String
Get
Return strState
End Get
Set(ByVal Value As String)
strState = Value
End Set
End Property
End Class
<Serializable()> Public Class Places
Inherits System.Collections.Specialized.NameObjectCollectio nBase
Public Sub New()
MyBase.New()
End Sub
Public Sub Add(ByVal Key As String, ByVal objPlace As Place)
MyBase.BaseAdd(Key, objPlace)
End Sub
Public Sub Remove(ByVal Key As String)
MyBase.BaseRemove(Key)
End Sub
Default Public ReadOnly Property Item(ByVal Key As Object) As Place
Get
If IsNumeric(Key) Then
Return MyBase.BaseGet(CType(Key, Integer))
Else
Return MyBase.BaseGet(CType(Key, String))
End If
End Get
End Property
End Class
Imports System.Runtime.Serialization.Formatters.Soap
Private Sub cmdTestSerialization_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button4.Click
Dim objPlaces As New Places()
Dim fs As System.IO.FileStream
Dim sf As SoapFormatter = New SoapFormatter()
objPlaces.Add("A", New Place("Moreno Valley", "CA"))
objPlaces.Add("B", New Place("Boston", "MA"))
objPlaces.Add("C", New Place("Moscow", "GA"))
fs = IO.File.Open("c:\myfile.xml", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite)
Try
sf.Serialize(fs, objPlaces)
Catch x As System.Runtime.Serialization.SerializationExceptio n
MessageBox.Show("Failed to serialize. Reason: " & x.Message)
Finally
fs.Close()
End Try
fs = Nothing
sf = Nothing
objPlaces = Nothing
fs = IO.File.Open("c:\myfile.xml", IO.FileMode.Open, IO.FileAccess.Read)
sf = New SoapFormatter()
Try
objPlaces = CType(sf.Deserialize(fs), Places) '<=======
ERROR LINE
Catch x As System.Runtime.Serialization.SerializationExceptio n
MessageBox.Show("Failed to deserialize. Reason: " & x.Message)
Catch y As Exception
MessageBox.Show("Error occured: " & y.Message)
Finally
fs.Close()
End Try
fs = Nothing
sf = Nothing
objPlaces = Nothing
End Sub 2 3631
Add the following constructor to your class (watch out for stupid line
breaks in the post:
--------------------------------
Protected Sub New(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext)
MyBase.New(info, context)
End Sub
------------------------------
This constructor is required by anthing that contains a hashtable like
object for its base as it contains special code to deserialize itself
instead of letting the framework do it. Anywhere you inherit from a class
that has a constructor like it, make sure you include this constructor in
your derived class.
Hope this helps,
Trev.
"Phillip Galey" <pg****@starcalif.com> wrote in message
news:eX*************@TK2MSFTNGP09.phx.gbl... I have an object called Place which contains only string properties and
has the <Serializable()> flag before the class name declaration. I also have
a collection object called Places, which is implemented using Inherits System.Collections.Specialized.NameObjectCollectio nBase and also has the <Serializable()> flag before the class name declaration.
In the calling code, I'm successfully serializing the object to an XML
file using a SoapFormatter object and a FileStream object. However, I am
running into a problem in deserializing the data back into the Places object. The deserialize method returns the error: "The constructor to deserialize an object of type TestingPad.Places was not found." Any ideas what might
cause the Deserialize method to return this error while the Serialize method
works flawlessly? Below is the code I'm using: <Serializable()> Public Class Place Private strCity As String Private strState As String Public Sub New() strCity = "" strState = "" End Sub Public Sub New(ByVal City As String, ByVal State As String) strCity = City strState = State End Sub Public Property City() As String Get Return strCity End Get Set(ByVal Value As String) strCity = Value End Set End Property Public Property State() As String Get Return strState End Get Set(ByVal Value As String) strState = Value End Set End Property End Class
<Serializable()> Public Class Places Inherits System.Collections.Specialized.NameObjectCollectio nBase Public Sub New() MyBase.New() End Sub Public Sub Add(ByVal Key As String, ByVal objPlace As Place) MyBase.BaseAdd(Key, objPlace) End Sub Public Sub Remove(ByVal Key As String) MyBase.BaseRemove(Key) End Sub Default Public ReadOnly Property Item(ByVal Key As Object) As Place Get If IsNumeric(Key) Then Return MyBase.BaseGet(CType(Key, Integer)) Else Return MyBase.BaseGet(CType(Key, String)) End If End Get End Property End Class
Imports System.Runtime.Serialization.Formatters.Soap Private Sub cmdTestSerialization_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button4.Click Dim objPlaces As New Places() Dim fs As System.IO.FileStream Dim sf As SoapFormatter = New SoapFormatter() objPlaces.Add("A", New Place("Moreno Valley", "CA")) objPlaces.Add("B", New Place("Boston", "MA")) objPlaces.Add("C", New Place("Moscow", "GA")) fs = IO.File.Open("c:\myfile.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite) Try sf.Serialize(fs, objPlaces) Catch x As System.Runtime.Serialization.SerializationExceptio n MessageBox.Show("Failed to serialize. Reason: " & x.Message) Finally fs.Close() End Try fs = Nothing sf = Nothing objPlaces = Nothing fs = IO.File.Open("c:\myfile.xml", IO.FileMode.Open,
IO.FileAccess.Read) sf = New SoapFormatter() Try objPlaces = CType(sf.Deserialize(fs), Places) '<======= ERROR LINE Catch x As System.Runtime.Serialization.SerializationExceptio n MessageBox.Show("Failed to deserialize. Reason: " & x.Message) Catch y As Exception MessageBox.Show("Error occured: " & y.Message) Finally fs.Close() End Try fs = Nothing sf = Nothing objPlaces = Nothing End Sub
Wonderful. Thanks. That worked.
"Codemonkey" <hu*********@hotmail.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl... Add the following constructor to your class (watch out for stupid line breaks in the post:
--------------------------------
Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
MyBase.New(info, context)
End Sub ------------------------------ This constructor is required by anthing that contains a hashtable like object for its base as it contains special code to deserialize itself instead of letting the framework do it. Anywhere you inherit from a class that has a constructor like it, make sure you include this constructor in your derived class.
Hope this helps,
Trev.
"Phillip Galey" <pg****@starcalif.com> wrote in message news:eX*************@TK2MSFTNGP09.phx.gbl... I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also
have a collection object called Places, which is implemented using Inherits System.Collections.Specialized.NameObjectCollectio nBase and also has the <Serializable()> flag before the class name declaration.
In the calling code, I'm successfully serializing the object to an XML file using a SoapFormatter object and a FileStream object. However, I am running into a problem in deserializing the data back into the Places object.
The deserialize method returns the error: "The constructor to deserialize
an object of type TestingPad.Places was not found." Any ideas what might cause the Deserialize method to return this error while the Serialize method works flawlessly? Below is the code I'm using: <Serializable()> Public Class Place Private strCity As String Private strState As String Public Sub New() strCity = "" strState = "" End Sub Public Sub New(ByVal City As String, ByVal State As String) strCity = City strState = State End Sub Public Property City() As String Get Return strCity End Get Set(ByVal Value As String) strCity = Value End Set End Property Public Property State() As String Get Return strState End Get Set(ByVal Value As String) strState = Value End Set End Property End Class
<Serializable()> Public Class Places Inherits System.Collections.Specialized.NameObjectCollectio nBase Public Sub New() MyBase.New() End Sub Public Sub Add(ByVal Key As String, ByVal objPlace As Place) MyBase.BaseAdd(Key, objPlace) End Sub Public Sub Remove(ByVal Key As String) MyBase.BaseRemove(Key) End Sub Default Public ReadOnly Property Item(ByVal Key As Object) As Place Get If IsNumeric(Key) Then Return MyBase.BaseGet(CType(Key, Integer)) Else Return MyBase.BaseGet(CType(Key, String)) End If End Get End Property End Class
Imports System.Runtime.Serialization.Formatters.Soap Private Sub cmdTestSerialization_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim objPlaces As New Places() Dim fs As System.IO.FileStream Dim sf As SoapFormatter = New SoapFormatter() objPlaces.Add("A", New Place("Moreno Valley", "CA")) objPlaces.Add("B", New Place("Boston", "MA")) objPlaces.Add("C", New Place("Moscow", "GA")) fs = IO.File.Open("c:\myfile.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite) Try sf.Serialize(fs, objPlaces) Catch x As System.Runtime.Serialization.SerializationExceptio n MessageBox.Show("Failed to serialize. Reason: " & x.Message) Finally fs.Close() End Try fs = Nothing sf = Nothing objPlaces = Nothing fs = IO.File.Open("c:\myfile.xml", IO.FileMode.Open, IO.FileAccess.Read) sf = New SoapFormatter() Try objPlaces = CType(sf.Deserialize(fs), Places) '<======= ERROR LINE Catch x As System.Runtime.Serialization.SerializationExceptio n MessageBox.Show("Failed to deserialize. Reason: " & x.Message) Catch y As Exception MessageBox.Show("Error occured: " & y.Message) Finally fs.Close() End Try fs = Nothing sf = Nothing objPlaces = Nothing End Sub
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kenneth Baltrinic |
last post by:
I am getting the following error when deserializing an object that has a couple of dozen dependant objects in its object graph. Anyone who can...
|
by: Philip Reed |
last post by:
I'm trying to write a preferences-handling infrastructure that
serializes prefs to XML. Basically I want to read in a common
"default" prefs set,...
|
by: tham |
last post by:
Hi
am trying to find a way to backward support serialized files of older version
Example:
Given a class
class MyClass
{
int a;
|
by: Marco Herrn |
last post by:
Hi,
I need to serialize an object into a string representation to store it
into a database. So the SOAPFormatter seems to be the right formatter...
|
by: Solel Software |
last post by:
Hello,
I am attempting to serialize an object for storage in a DB field and them deserialize it later on. I have managed to Serialize it using
...
|
by: Solel Software |
last post by:
Hello,
I am attempting to serialize an object for storage in a DB field and them deserialize it later on. I have managed to Serialize it using
...
|
by: Chris Newby |
last post by:
Given:
public class MyClass{
public String MyPropertyOne;
}
I have a soap document created by serializing an instance of a previous
version...
|
by: Christian Wilhelm |
last post by:
Hi!
I have a Problem understanding the deserialisation of SOAP-Responses.
The "normal" way, calling a XML WebService is to use WSDL
1) Send...
|
by: news |
last post by:
I am porting some code from a PC to a PocketPC. I want to read data
from an XML-file into a hashtable. But since the soapformatter does not
exist...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |