Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

When I try to read an XML file from my Vb.net project, it gives an error saying "AN UNHANDLED EXCEPTION OF TYPE SYSTEM.INVALIDOPERATIONEXCEPTION OCCURRED IN SYSTEM.XML.DLL.

ADDITIONAL INFO: THERE IS AN ERROR IN XML DOCUMENT (2,2)." on the deserialize line. There is no problem with the XML file. I'm able to read the same file thro another application. I've tried everything but it still persists. The project where it is giving an error is migrated from VB6.0, if that matters. Please help!

Posted (edited)

Public Shared Function Load(ByVal filename As String, ByVal newType As Type) As Object

       Dim fileinfo As New FileInfo(filename)
       If fileinfo.Exists = False Then
           Return System.Activator.CreateInstance(newtype)
       End If

       Dim stream As New FileStream(filename, FileMode.Open)
       Dim newobj As Object = Load(stream, newType)

       stream.Close()

       Return newobj

End Function

Public Shared Function Load(ByVal stream As Stream, ByVal newtype As Type) As Object

       Dim Serializer As New XmlSerializer(newtype)
       Dim newobj As Object = Serializer.Deserialize(stream)

       Return newobj

End Function

'The above are the two serializing functions. The error is coming
'in the second function when it's tring to deserialize. I'm getting
'the file name from a data file name property

Public ReadOnly Property DataFilename() As String
       Get
           Dim folder As String
           folder = Environment.CurrentDirectory
           Return folder & "\surveyor.xml"
       End Get
End Property

[edit]added [ vb ] tags [/ vb ] [/edit]

Edited by Derek Stone
  • *Gurus*
Posted
You really should be declaring newobj as the exact type the XML file will be deserialized into. As it stands currently, you're trying to read it into an Object. Also keep in mind that not everything serializes to XML correctly. Certain classes and objects just won't be formatted correctly.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...