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>