Results 1 to 3 of 3

Thread: [RESOLVED] XML Deserialization Problem

  1. #1

    Thread Starter
    Lively Member despotovski01's Avatar
    Join Date
    Apr 2011
    Posts
    100

    Resolved [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:
    1. Public Sub Form1_Load(ByVal sender as Object, ByVal e as EventArgs) Handles Me.Load
    2.  Dim path As String = AppDataPath & "\D01 Software Manager\XML"
    3.         If IO.Directory.Exists(path) = True Then
    4.             Dim intIndex As Integer = 0
    5.             For Each strFile As String In GetFiles(path)
    6.                 Dim bar As New ProgramBar
    7.                 bar.ID = intIndex
    8.                 bar.Info = LoadInfo(strFile, GetType(ProgramInfo))
    9.                 'bar.picIcon.Image = ReturnIcon(bar.Info.exe, 0).ToBitmap
    10.                 barList.Add(bar)
    11.                 intIndex += 1
    12.             Next
    13.         End If
    14.         If barList.Count <> 0 Then
    15.             For Each bar As ProgramBar In barList
    16.                 Me.Controls.Add(bar)
    17.                 bar.Location = New Point(20, 200)
    18.                 bar.Size = New Size(544, 100)
    19.                 bar.Parent = FlowLayoutPanel1
    20.             Next
    21.         Else
    22.             Dim lblInfo As New Label
    23.             lblInfo.ForeColor = Color.Gray
    24.             lblInfo.BackColor = Color.Transparent
    25.             lblInfo.Text = "You don't have programs from D01 MicroApps."
    26.             Me.Controls.Add(lblInfo)
    27.             lblInfo.Parent = FlowLayoutPanel1
    28.             lblInfo.AutoSize = True
    29.         End If
    30. End Sub
    31.  
    32. Public Shared Function LoadInfo(ByVal filename As String, ByVal newType As Type) As Object
    33.         'Does the file exist?
    34.         Dim fileInfo As New FileInfo(filename)
    35.         If fileInfo.Exists = False Then
    36.             'create a blank version of the object and return that..
    37.             Return System.Activator.CreateInstance(newType)
    38.         End If
    39.         'open the file
    40.         Dim stream As New FileStream(filename, FileMode.Open)
    41.         'load the object from the stream..
    42.         Dim newObject As Object = LoadInfo(filename, newType)
    43.         'Close the stream
    44.         stream.Close()
    45.         'return the object..
    46.         Return newObject
    47.     End Function
    48.  
    49.     'Load-actually perform the deserialization
    50.     Public Shared Function LoadInfo(ByVal stream As Stream, ByVal newType As Type) As Object
    51.         'create a serializer and load the object..
    52.         Dim serializer As New XmlSerializer(newType)
    53.         Dim newObject As Object = serializer.Deserialize(stream)
    54.         'return the new object
    55.         Return newObject
    56.     End Function

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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.

  3. #3

    Thread Starter
    Lively Member despotovski01's Avatar
    Join Date
    Apr 2011
    Posts
    100

    Re: XML Deserialization Problem

    Quote Originally Posted by Grimfort View Post
    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!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width