如何在restful Web服务中传递复杂对象

时间:2014-06-11 10:15:35

标签: vb.net web-services

我需要传递一个对象(如此定义的结构列表):

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

从表单(vb.net)到restful Web服务。我该怎么办?

2 个答案:

答案 0 :(得分:2)

使用JSON或XML将其传递给restful服务。

JSON on .NET

XML-Serializer

答案 1 :(得分:0)

我正在使用XML-Serializer。我有两个功能:一个客户端:

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 

和一个用于web-service。我从Web服务获取xml文件作为字符串并尝试使用此函数对其进行反序列化:

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

但是在反序列化上我获得了一个InvalidOperationException。 (xml文档中的错误)