[RESOLVED] XML Deserialization Problem
Hi guys. I have a problem deserializing XML files. The deserialization must be done in a For Loop, but it gives me an IOException saying that the file is used by another process. This happens only when I try to deserialize XML files in a For Loop. When not in loop, it deserializes the XML normally. What can cause the problem here? Here's my code:
vb.net Code:
Public Sub Form1_Load(ByVal sender as Object, ByVal e as EventArgs) Handles Me.Load
Dim path As String = AppDataPath & "\D01 Software Manager\XML"
If IO.Directory.Exists(path) = True Then
Dim intIndex As Integer = 0
For Each strFile As String In GetFiles(path)
Dim bar As New ProgramBar
bar.ID = intIndex
bar.Info = LoadInfo(strFile, GetType(ProgramInfo))
'bar.picIcon.Image = ReturnIcon(bar.Info.exe, 0).ToBitmap
barList.Add(bar)
intIndex += 1
Next
End If
If barList.Count <> 0 Then
For Each bar As ProgramBar In barList
Me.Controls.Add(bar)
bar.Location = New Point(20, 200)
bar.Size = New Size(544, 100)
bar.Parent = FlowLayoutPanel1
Next
Else
Dim lblInfo As New Label
lblInfo.ForeColor = Color.Gray
lblInfo.BackColor = Color.Transparent
lblInfo.Text = "You don't have programs from D01 MicroApps."
Me.Controls.Add(lblInfo)
lblInfo.Parent = FlowLayoutPanel1
lblInfo.AutoSize = True
End If
End Sub
Public Shared Function LoadInfo(ByVal filename As String, ByVal newType As Type) As Object
'Does the file exist?
Dim fileInfo As New FileInfo(filename)
If fileInfo.Exists = False Then
'create a blank version of the object and return that..
Return System.Activator.CreateInstance(newType)
End If
'open the file
Dim stream As New FileStream(filename, FileMode.Open)
'load the object from the stream..
Dim newObject As Object = LoadInfo(filename, newType)
'Close the stream
stream.Close()
'return the object..
Return newObject
End Function
'Load-actually perform the deserialization
Public Shared Function LoadInfo(ByVal stream As Stream, ByVal newType As Type) As Object
'create a serializer and load the object..
Dim serializer As New XmlSerializer(newType)
Dim newObject As Object = serializer.Deserialize(stream)
'return the new object
Return newObject
End Function
Re: XML Deserialization Problem
One obvious issue is this:
'load the object from the stream..
Dim newObject As Object = LoadInfo(filename, newType)
You are calling the same function in a loop. Although you have 2 with the same name and different signatures, you are not passing the correct parameters to get the 2nd method.
Dim newObject As Object = LoadInfo(stream , newType) '<< pass the stream, not the string?
You could tell this yourself, if you either step through your code, or if you were to use the call stack to see which method calls which method.
Re: XML Deserialization Problem
Quote:
Originally Posted by
Grimfort
One obvious issue is this:
'load the object from the stream..
Dim newObject As Object = LoadInfo(filename, newType)
You are calling the same function in a loop. Although you have 2 with the same name and different signatures, you are not passing the correct parameters to get the 2nd method.
Dim newObject As Object = LoadInfo(stream , newType) '<< pass the stream, not the string?
You could tell this yourself, if you either step through your code, or if you were to use the call stack to see which method calls which method.
Thank you a lot man! You fixed my problem! You're great! :D