【问题标题】:XML list types required for WCFWCF 所需的 XML 列表类型
【发布时间】:2012-09-24 05:30:32
【问题描述】:

我正在将 xml 数据的单个帖子从 android 应用程序 (java) 发送到 .net 4 WCF Web 服务。 xml创建如下

  xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  xmlBuilder.append("<LogDeviceCommunication
  xmlns=\"http://schemas.datacontract.org/2004/07/conxEntities\">");
  xmlBuilder.append("<DeviceID>").append(DeviceID).append("</DeviceID>");
  xmlBuilder.append("<ID>").append(ID).append("</ID>");
  xmlBuilder.append("<Info>").append(Info).append("</Info>");
  xmlBuilder.append("<Line>").append(Line).append("</Line>");
  xmlBuilder.append("<Tab>").append(Tab).append("</Tab>");
  xmlBuilder.append("<Time>").append(new DateTime(Time).toDateTimeISO()).append
  ("</Time>");
  xmlBuilder.append("</LogDeviceCommunication>");

WCF方法如下

  [OperationContract]
  [WebInvoke(UriTemplate = "AddDeviceCommunicationLog", RequestFormat = 
  WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
  string AddDeviceCommunicationLog(LogDeviceCommunication deviceCommunicationEntry);

我想更改为发送 LogDeviceCommunication 列表以加快速度,但不确定将什么用作父元素,即

<parent element>
  <logdevicecommunication>...</logdevicecommunication>
  <logdevicecommunication>...</logdevicecommunication>
  <logdevicecommunication>...</logdevicecommunication>
</parent element>

对 WCF 的调用通常会返回一个 List 作为 ArrayOf.... 但发布时自然不存在这种类型。 我需要创建消息合同或类似的吗?

谢谢

【问题讨论】:

    标签: xml wcf rest


    【解决方案1】:

    您只需要一个集合数据合同类型,例如:

    [CollectionDataContract(Name="WhateverYouWantToCallTheRootElement")]
    public class LogDeviceCommunications: Collection<LogDeviceCommunication>  {}
    

    然后更改您的操作方法以采用这种新类型的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      相关资源
      最近更新 更多