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 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
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 suggest where I might begin to look to resolve problem...
|
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...
|
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
for this purpose. Now I have the problem that...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |