Reading XML from VB.Net

whizkid123

Active member
Joined
Dec 11, 2002
Messages
33
Location
India
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. Im able to read the same file thro another application. Ive tried everything but it still persists. The project where it is giving an error is migrated from VB6.0, if that matters. Please help!
 
Code:
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 its tring to deserialize. Im 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]
 
Last edited by a moderator:
You really should be declaring newobj as the exact type the XML file will be deserialized into. As it stands currently, youre trying to read it into an Object. Also keep in mind that not everything serializes to XML correctly. Certain classes and objects just wont be formatted correctly.
 
Back
Top