troubleshooting Question

How do I deserializea string to an object in vb?

Avatar of Spike UK
Spike UKFlag for United Kingdom of Great Britain and Northern Ireland asked on
XMLVisual Basic.NET* serialization
6 Comments1 Solution157 ViewsLast Modified:
I have a string being returned from a HttpWebRequest.
The string returns in the format:
"{""result"":{""version"":2,""name"":""StatusResult"",""database"":""ok"",""licenses"":""ok"",""internal_services"":""ok"",""reporting_service"":""ok""}}"
I want to save this to an object by deserializing it.

I have created an object:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Imports System.Xml.Serialization

<Serializable()>
Public Class Status
    Public version As Integer
    Public name As String
    Public database As String
    Public licenses As String
    Public internal_services As String
    Public reporting_service As String


    ' Empty constructor required for serialization.
    Public Sub New()
    End Sub

    Public Sub New(ByVal new_version As Integer,
                   ByVal new_name As String,
                   ByVal new_database As String,
                   ByVal new_licenses As String,
                   ByVal new_internal_services As String,
                   ByVal new_reporting_service As String)

        version = new_version
        name = new_name
        database = new_database
        licenses = new_licenses
        internal_services = new_internal_services
        reporting_service = new_reporting_service
    End Sub
End Class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
but receive an error when running the following:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
url = String.Format("http://127.0.0.1:8600/status/")
            res = String.Empty
            serviceRequest = New WebClient()


            Dim stat As New Status(
                version,
                name,
                database,
                licenses,
                internal_services,
                reporting_service)

            Dim xml_serializer As New XmlSerializer(GetType(Status))
            Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

            res = serviceRequest.DownloadString(New Uri(url))

            Dim string_reader As New StringReader(res)

            stat = xml_serializer.Deserialize(string_reader)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error= "There is an error in XML document (1, 1)."

I'm presuming it's because of the xml tag "Result" but how do I deal with it?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros