473,832 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error Deserializing NameObjectColle ction 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.Collecti ons.Specialized .NameObjectColl ectionBase 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.Plac es 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.Collecti ons.Specialized .NameObjectColl ectionBase
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.BaseRemo ve(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.F ormatters.Soap
Private Sub cmdTestSerializ ation_Click(ByV al sender As System.Object, ByVal
e As System.EventArg s) Handles Button4.Click
Dim objPlaces As New Places()
Dim fs As System.IO.FileS tream
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.Ope nOrCreate,
IO.FileAccess.R eadWrite)
Try
sf.Serialize(fs , objPlaces)
Catch x As System.Runtime. Serialization.S erializationExc eption
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.Ope n, IO.FileAccess.R ead)
sf = New SoapFormatter()
Try
objPlaces = CType(sf.Deseri alize(fs), Places) '<=======
ERROR LINE
Catch x As System.Runtime. Serialization.S erializationExc eption
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 3777
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.S erializationInf o, ByVal context As
System.Runtime. Serialization.S treamingContext )

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****@starcal if.com> wrote in message
news:eX******** *****@TK2MSFTNG P09.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.Collecti ons.Specialized .NameObjectColl ectionBase 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.Plac es 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.Collecti ons.Specialized .NameObjectColl ectionBase
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.BaseRemo ve(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.F ormatters.Soap
Private Sub cmdTestSerializ ation_Click(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Button4.Click
Dim objPlaces As New Places()
Dim fs As System.IO.FileS tream
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.Ope nOrCreate,
IO.FileAccess.R eadWrite)
Try
sf.Serialize(fs , objPlaces)
Catch x As System.Runtime. Serialization.S erializationExc eption
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.Ope n, IO.FileAccess.R ead) sf = New SoapFormatter()
Try
objPlaces = CType(sf.Deseri alize(fs), Places) '<=======
ERROR LINE
Catch x As System.Runtime. Serialization.S erializationExc eption
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*********@ho tmail.com> wrote in message
news:OD******** ******@TK2MSFTN GP09.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.S erializationInf o, ByVal context As
System.Runtime. Serialization.S treamingContext )

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****@starcal if.com> wrote in message
news:eX******** *****@TK2MSFTNG P09.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.Collecti ons.Specialized .NameObjectColl ectionBase 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.Plac es 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.Collecti ons.Specialized .NameObjectColl ectionBase
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.BaseRemo ve(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.F ormatters.Soap
Private Sub cmdTestSerializ ation_Click(ByV al sender As System.Object,

ByVal
e As System.EventArg s) Handles Button4.Click
Dim objPlaces As New Places()
Dim fs As System.IO.FileS tream
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.Ope nOrCreate,
IO.FileAccess.R eadWrite)
Try
sf.Serialize(fs , objPlaces)
Catch x As System.Runtime. Serialization.S erializationExc eption
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.Ope n,

IO.FileAccess.R ead)
sf = New SoapFormatter()
Try
objPlaces = CType(sf.Deseri alize(fs), Places) '<=======
ERROR LINE
Catch x As System.Runtime. Serialization.S erializationExc eption
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
2231
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 I would greatly in debted to. Serializing the object works fine. When I try to deserialize it, I get the following error: A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional...
0
2092
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 defaults as appropriate. I'm trying to do them both using the SoapFormatter to serialize. But when I try to read in the user's prefs after reading the defaults, both from similar Soap files, I get this exception: ----...
2
1435
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
13362
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 this formatter writes into a stream. And I am not used enough to C# to convert this to a string. I tried the following code: MemoryStream stream= new MemoryStream() ; IFormatter formatter = new SoapFormatter();
1
1309
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 ObjectToSerialize) { MemoryStream ms = new MemoryStream(); string theSerializedObject; System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
5
2143
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 ObjectToSerialize) { MemoryStream ms = new MemoryStream(); string theSerializedObject; System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
0
1272
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: public class MyClass{
4
7851
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 out of WSDL transform the SOAP-Response to an Object. Let us call it "Customer".
0
1203
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 workaround. I have tried to use xmlserializer, but it always wants me to supply it with a type. If I use the type of hashtable I get an exception. Any help or hints would be highly appreciated. Below is the code as it more or less looks in my program :
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4421
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.