1

我如何定义消息契约来获得这种 XML SOAP 格式?

架构:

    <List>
      <Exclusions>
        <ExclusionID>123</ExclusionID>
        <ExclusionID>656</ExclusionID>
      </Exclusions>
      </List>

我创建了如下所述的类文件,但它给出了不同的响应

<DataContract> _
Public Class List


    ''' <remarks/>
    Private _exclusions As Exclusions

    <DataMember()> _
    Public Property Exclusions() As Exclusions
        Get
            Return _exclusions
        End Get
        Set(ByVal value As Exclusions)
            _exclusions = value
        End Set
    End Property

End Class

<DataContract>
Public Class Exclusions

    <DataMember>
    Public ExclusionID As ExclusionID()

End Class

<MessageContract>
Public Class ExclusionID

    <MessageBodyMember(Name:="")>
   Public Value As String

End Class

我的回复是这样的:

    <List >
      <Exclusions >
        <ExclusionID>
          <ExclusionID>
            <Value>123</Value>
          </ExclusionID>
          <ExclusionID>
            <Value>145</Value>
          </ExclusionID>
        </ExclusionID>
      </Exclusions>
    </List>

如何使用消息合约获得正确的消息?

4

1 回答 1

0

我的 VS 中没有安装 VB.net。下面的 C# 代码供您参考。

//类定义

public class List {

    [MessageBodyMember]
    public Exclusions Exclusions { get; set; }
}

[CollectionDataContract(Name="Exclusions", ItemName="Exclusion")]
public class Exclusions : List<String> {

}

//help method
static void WriteMessage(Message message, String filePath) {
    using (var writer = new XmlTextWriter(filePath, Encoding.UTF8)) {
        message.WriteMessage(writer);
    }
}

//code
var myList = new List();
myList.Exclusions = new Exclusions();
myList.Exclusions.Add("123");
myList.Exclusions.Add("656");

using (var message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10,
    "http://127.0.0.1:3333/someaction", myList)) {

    WriteMessage(message, "a.xml");
}
于 2014-04-04T12:09:33.650 回答