Luca F.
Luca F.

Reputation: 113

How to pass complex object in restful web service

I need to pass an object (a list of structures so defined):

Private attivitaDaTrasferire As New List(Of FileDaTrasferire)

Private Structure FileDaTrasferire
    Dim activityID As Integer
    Dim DataIns As Date
    Dim idUtenteComp As Integer
    Dim idVersione As Integer
    Dim idFile As Integer
    Dim fileNome As String
    Dim fileDestinazione As String
    Dim fileTipoProdotto As String
    Dim fileTipo As String

    Dim fileBinarySize As Integer
    Dim fileBinaryDate As String
    Dim fileBinary As Long
End Structure

from a form (vb.net) to a restful web service. How can I do?

Upvotes: 0

Views: 557

Answers (2)

Luca F.
Luca F.

Reputation: 113

I'm using XML-Serializer. I have two functions: one client side:

Private Function SerializeActivity(singolaAttivitaDaTrasferire As List(Of FileDaTrasferire)) As String
        Dim writer As New StringWriter()
        Dim serializer As New XmlSerializer(GetType(List(Of FileDaTrasferire)))

        Try
            serializer.Serialize(writer, singolaAttivitaDaTrasferire)
        Catch ex As Exception
            Console.WriteLine("Eccezione " + ex.Message)
        End Try

        Return writer.ToString()
    End Function 

and one for the web-service.I get the xml file from the web service as a string and try to deserialize it with this function:

Public Function SaveDataPost(sXMLFile As String) As Boolean Implements ILiveUpdateWS.SaveDataPost

        Dim reader As New StringReader(sXMLFile)
        Dim serializer As New XmlSerializer(GetType(List(Of FileDaAggiornare)))
        Dim myFile As List(Of FileDaAggiornare)
        'Dim myFile As FileDaAggiornare
        Try
            myFile = serializer.Deserialize(reader)
        Catch ex As Exception
            Console.WriteLine("Eccezione: " + ex.Message)
        End Try

        Return Nothing
    End Function

but on the deserialize I obtain an InvalidOperationException. (Error in the xml document)

Upvotes: 0

Fabian Bigler
Fabian Bigler

Reputation: 10895

Either use JSON or XML for passing it to the restful service.

JSON on .NET

XML-Serializer

Upvotes: 2

Related Questions