473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Error Deserializing NameObjectCollection Object Using SoapFormatter

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

Nov 20 '05 #1
2 3731
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

Nov 20 '05 #2
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


Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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 suggest where I might begin to look to resolve problem...
0
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, then read in the user's prefs and override the...
2
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;
6
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 for this purpose. Now I have the problem that...
1
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 private string SerializeObject(object...
5
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 private string SerializeObject(object...
0
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 of MyClass. However, now MyClass looks like: ...
4
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 Request 2) Get SOAP-Response 3) With Informations...
0
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 in the compact framework I have to find a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.