469,578 Members | 1,322 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

Problem Serializing Multiple Objects to the Same Stream with SoapFormatter

I'm trying to serialize a collection to a file stream by serializing each object
individually. The code below works fine with the BinaryFormatter, but the
SoapFormatter reads the first object and just goes to the end of the file. After
reading the first object, the fStream.Position is pointed to the end of the file.
The collection serializes/deserilizes fine when I just serialize the hashtable vs.
each object in the hashtable.

Does anyone see anything wrong with the serialize/deserialize code below?

Thanks in Advance - JackRazz

'Deserialize
Private Sub DoLoadFromFile(ByVal FileName As String)
Dim file As System.IO.File

'Initilize
Dim obj As Object

'Open stream for Input
Dim fStream As FileStream = New FileStream(FileName, FileMode.Open)
Dim streamLen As Integer = fStream.Length

Select Case FileFormat
Case PropSaver.FileFormats.Binary
Dim bFormatter As New BinaryFormatter()

'Deserialize the contents of the File Stream (me.InnerList)
Do While fStream.Position < streamLen
obj = bFormatter.Deserialize(fStream)
Me.Add(obj)
Loop

Case PropSaver.FileFormats.XML
Dim sFormatter As SoapFormatter = New SoapFormatter(Nothing, New
StreamingContext(StreamingContextStates.File))

'Deserialize the contents of the File Stream (me.InnerList)
Do While fStream.Position < streamLen
obj = sFormatter.Deserialize(fStream)
Me.Add(obj)
Loop

End Select
fStream.Close()

End Sub
'Serialize
Private Sub DoSaveToFile(ByVal FileName As String)

Dim file As System.IO.File

If (Not FileName Is Nothing) And (FileName.Length > 0) Then
Dim fStream As System.IO.Stream = file.Open(FileName, IO.FileMode.Create)

Select Case FileFormat
Case PropSaver.FileFormats.Binary
Dim bFormatter As New BinaryFormatter()

'Deserialize the contents of the File Stream (me.InnerList)
Dim obj As Object
For Each obj In AllSettings
bFormatter.Serialize(fStream, obj.value)
Next obj

Case PropSaver.FileFormats.XML
Dim sFormatter As New SoapFormatter(Nothing, New
StreamingContext(StreamingContextStates.File))

'Deserialize the contents of the File Stream (me.InnerList)
Dim obj As Object
For Each obj In AllSettings 'AllSettings is a hashtable
sFormatter.Serialize(fStream, obj.value)
Next obj

End Select
fStream.Close()
End If

End Sub
Contents of xml file with 2 objects
-----------------------------------------------------------------------
<SOAP-ENV:Envelope >
<SOAP-ENV:Body>
<a1:TextBoxSettings id="ref-1" >
<Text id="ref-3">Jack</Text>
<ControlName id="ref-4">TextBox1</ControlName>
<SettingType>ControlSetting</SettingType>
<TypeName id="ref-5">TextBox</TypeName>
<ControlSettingsBase_x002B_mKey
id="ref-6">Form1.TextBox1</ControlSettingsBase_x002B_mKey>
<ControlSettingsBase_x002B_mFormName
id="ref-7">Form1</ControlSettingsBase_x002B_mFormName>
<SettingsBase_x002B_mKey id="ref-8"></SettingsBase_x002B_mKey>
<SettingsBase_x002B_mFormName xsi:null="1"/>
</a1:TextBoxSettings>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<SOAP-ENV:Envelope ...>
<SOAP-ENV:Body>
<a1:FormSettings id="ref-1" ...>
<Top>92</Top>
<Left>91</Left>
<Width>153</Width>
<Height>166</Height>
<WindowState>Normal</WindowState>
<SaveWindowState>false</SaveWindowState>
<ControlName id="ref-4">Form1</ControlName>
<SettingType>ControlSetting</SettingType>
<TypeName id="ref-5">Form</TypeName>
<ControlSettingsBase_x002B_mKey
id="ref-6">Form1.Form1</ControlSettingsBase_x002B_mKey>
<ControlSettingsBase_x002B_mFormName href="#ref-4"/>
<SettingsBase_x002B_mKey id="ref-7"></SettingsBase_x002B_mKey>
<SettingsBase_x002B_mFormName xsi:null="1"/>
</a1:FormSettings>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


Nov 20 '05 #1
0 1408

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Scott Meddows | last post: by
2 posts views Thread by Eric | last post: by
6 posts views Thread by Rein Petersen | last post: by
2 posts views Thread by Tobias Zimmergren | last post: by
1 post views Thread by Jax | last post: by
1 post views Thread by Bsiang Tan | last post: by
1 post views Thread by Scott Meddows | last post: by
9 posts views Thread by norvinl | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.