1

使用 WCF 如何修改此 DataContract 或代码:

<DataContract()>
Public Class listaActos
Private _codigoActo As List(Of String)

<DataMember(IsRequired:=True)>
Public Property codigoActo() As List(Of String)
    Get
        Return _codigoActo
    End Get

    Set(ByVal value As List(Of String))
        _codigoActo = value
    End Set
End Property   ...

序列化时生成以下响应xml:

 <a:listaActos>
     <a:codigoActo xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
            <b:string>01672</b:string>
     </a:codigoActo>  </a:listaActos>

但我想博如下:

<a:listaActos>
     <a:codigoActo>01672</a:codigoActo>
     <a:codigoActo>01673</a:codigoActo>
     <a:codigoActo>01674</a:codigoActo></a:listaActos>

提前致谢!!

4

1 回答 1

1

如果您的类listaActos只有其中的列表codigoActo,那么您可以将其设为集合类型(例如,使其继承自List(Of String))。这将为您提供所需的 XML 形状DataContractSerializer

如果它有更多成员,那么您需要使用XmlSerializer, 并这样定义类。

下面的代码显示了列表选项。

Public Class StackOverflow_10554490
    <CollectionDataContract(Name:="listaActos", Namespace:="http://my.namespace", ItemName:="codigoActo")> _
    Public Class listaActos
        Inherits List(Of String)
    End Class

    <DataContract(Name:="Root", Namespace:="http://my.namespace")> _
    Public Class Root
        <DataMember()> _
        Public Property listaActos As listaActos
    End Class

    Public Shared Sub Test()
        Dim list As listaActos = New listaActos()
        list.Add("01672")
        list.Add("01673")
        list.Add("01674")
        Dim root = New Root With {.listaActos = list}
        Dim dcs = New DataContractSerializer(GetType(Root))
        Dim ms = New MemoryStream()
        dcs.WriteObject(ms, root)
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()))
    End Sub
End Class
于 2012-05-11T16:05:51.703 回答