0

我已经创建了一个 Web 服务看起来像

Public Class NameAndvalue
    Public name As String
    Public value As String
End Class
<WebMethod()> _
Public Function ReturnNames_1() As ArrayList
    Dim names As New List(Of NameAndvalue)
    Dim nv As NameAndvalue
    nv = New NameAndvalue With {.name = "AAA", .value = "1"} : names.Add(nv)
    nv = New NameAndvalue With {.name = "BBB", .value = "2"} : names.Add(nv)
    nv = New NameAndvalue With {.name = "CCC", .value = "3"} : names.Add(nv)
    Dim Arr As ArrayList = New ArrayList(names)
    Return Arr
End Function

当我在浏览器中调用时(给我一个错误......)

System.InvalidOperationException:生成 XML 文档时出错。---> System.InvalidOperationException:类型 TMR.AndroidMethods+NameAndvalue 不是预期的。使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型。在 System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(字符串名称,字符串 ns,对象 o,布尔 xsiType)在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(字符串 n,字符串 ns,对象 o,布尔 isNullable,布尔需要类型) 在 Microsoft.Xml.Serialization.GeneratedAssembly.ArrayListSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer) 在 System.Xml 的 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_ArrayOfAnyType(Object o)。

知道这个错误我在返回list(of string)from 方法时看到了它,因为我改变了从List(Of String)To的返回ArrayList但发生了同样的问题。

问题是什么 ??

4

1 回答 1

1

尝试List(Of NameAndvalue)改用。下面的例子

<WebMethod()> _
Public Function HelloWorld() As List(Of NameAndvalue)
    Dim names As New List(Of NameAndvalue)
    Dim nv As NameAndvalue
    nv = New NameAndvalue With {.name = "AAA", .value = "1"} : names.Add(nv)
    nv = New NameAndvalue With {.name = "BBB", .value = "2"} : names.Add(nv)
    nv = New NameAndvalue With {.name = "CCC", .value = "3"} : names.Add(nv)

    Return names
End Function

返回

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfNameAndvalue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <NameAndvalue>
    <name>AAA</name>
    <value>1</value>
  </NameAndvalue>
  <NameAndvalue>
    <name>BBB</name>
    <value>2</value>
  </NameAndvalue>
  <NameAndvalue>
    <name>CCC</name>
    <value>3</value>
  </NameAndvalue>
</ArrayOfNameAndvalue>
于 2012-11-27T18:58:06.567 回答