如何使用自定义类从Web服务返回数据

时间:2011-02-11 00:43:32

标签: xml vb.net xml-serialization xsd

所以我有一个使用web服务的应用程序。在UI端,用户选择一个表,该表通过Web向我的webservice发送请求以返回该表的字段。

因此,在完成此请求后,我在Web服务端上有一系列名称,类型对,我需要序列化并发送回我的客户端。我可以使用List(String())来返回值,但我想使用List(Custom_Class),因为稍后我必须在更大的范围内执行此操作,并且我想学习这个更简单的版本。

现在我从研究中知道我可以从.XSD文档创建一个类,所以我想我应该做的就是将这个类转换为.XSD,并以某种方式将我的请求发送到WebService,以便它可以使用.XSD在运行时动态创建类,将该类实例化为对象,用结果集填充这些对象,将其捆绑到强类型List(ReturnFields)中,将其序列化为XML,然后将其发送回我的客户端,将其反序列化并显示给他。

所以2个问题: 我在这里走在正确的轨道上还是我走错了路。 2.如何将.XSD架构传递给我的Web服务,以便它可以使用它在服务器端创建类?


-------------- EDIT ----------- 所以基于pMartin的输入我正在修改这个问题。

以下是我现在的代码。

我的图书馆图层中有“字段”类

Public Class Fields
    Private Field_Name As String
    Private Field_Type As System.Type

    Public Sub New()
    End Sub

    Protected Overrides Sub Finalize()
    End Sub

    Property Name() As String
        Get
            Return Field_Name
        End Get
        Set(ByVal value As String)
            Field_Name = value
        End Set
    End Property

    Property Type() As System.Type
        Get
            Return Field_Type
        End Get
        Set(ByVal value As System.Type)
            Field_Type = value
        End Set
    End Property
End Class

通过以下代码在我的库中使用此get。

Public Function Get_List_Of_Fields(ByVal Table_Name As String, ByVal Database_Name As String) As List(Of Fields)
    Dim List_Of_Fields As New List(Of Fields)
    Dim DataModel As MetaModel = Get_Datamodel(Database_Name)

    Dim Data_Table = (From DataTable In DataModel.GetTables Where DataTable.TableName = Table_Name).FirstOrDefault

    For Each Field In Data_Table.RowType.DataMembers
        Dim Fields_Obj As New Fields
        Fields_Obj.Name = Field.Name
        Fields_Obj.Type = Field.Type
        List_Of_Fields.Add(Fields_Obj)
    Next
    Return List_Of_Fields
End Function

返回我的Web服务(.ASMX)

<WebMethod()> _
Public Function Get_List_of_Fields(ByVal Table_Name As String, ByVal Database_Name As String) As List(Of Fields)
    Return _DynaLib.Get_List_Of_Fields(Table_Name, Database_Name)
End Function

返回给我的客户 '这是我的错误在这个“返回”行

Imports Dynamic_Charting.DynamicReference 
Private _DynamicService As New DynamicServiceSoapClient

Public Function Get_List_of_Fields(ByVal Table_name As String, ByVal Database_Name As String) As List(Of Fields)
     Return _DynamicService.Get_List_of_Fields(Table_name, Database_Name)
 End Function

这是我的错误。我收到以下错误。

错误11
类型'Dynamic_Charting.DynamicReference.Fields的1维数组'的值无法转换为 'System.Collections.Generic.List(Of Dynamic_Charting.DynamicReference.Fields)'。

1 个答案:

答案 0 :(得分:0)

我认为您不需要在客户端和Web服务之间来回传递XML架构。如果要使用自定义类来发送输出数据,可以将自定义类设置为Web服务方法的返回类型。

基本上,对于您的Web服务方法,您会有这样的定义(假设您还有一个名为InputFields的类来捕获有关用户选择的表的数据):

<WebMethod()>
Public Function MyWebMethod(inputData As InputFields) As ReturnFields
    ...
End Function