我正在尝试使用特定形式的XML来回复我的web服务,我认为只需将数据放入字符串并将其返回到网页即可。
我想回来:
<tradeHistory><trade TradeId="1933" ExtId="1933" instrument="EUA" quantity="1500" setType="Escrow" TradeDate="12/02/2010" DeliveryDt="13/02/2010" PaymentDt="12/02/2010" type="BUY" pricePerUnit="6.81" GrossConsid="10320" currency="EUR"/></tradeHistory>
但是当我返回我正在收到的字符串时:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><tradeHistory><trade tradeid="1933" ExtId="1933" Instrument="EUA" quantity"1500" setType="Escrow" TradeDate="24/05/2011" DeliveryDt="25/05/2011" PaymentDt="25/05/2011" type"BUY" pricePerUnit="6.81" GrossConsid="10320" currency="EUR" /><tradeHistory></string>
关于如何实现这一目标的任何想法?如果没有标签会很好,但我可以忍受这个,但我遇到的问题是它没有正确格式化字符串,它将开始和结束标签作为特殊字符读取
我的服务是:
<ServiceContract(Namespace:="")>
Public Interface ITradePortal
<WebGet(UriTemplate:="Reporting/GetClientTrades/{ClientID}")>
<OperationContract()>
Function GetClientTrades(ByVal ClientID As String) As String
End Interface
我的实施是:
<ServiceBehavior(ConcurrencyMode:=System.ServiceModel.ConcurrencyMode.Multiple, InstanceContextMode:=InstanceContextMode.Single, _
Namespace:="")>
<XmlSerializerFormat()>
和我的配置文件:
<services>
<service behaviorConfiguration="Default" name="CFP_Web_Lib.TradePortal">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8686/TradePortal"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding"
contract="CFP_Web_Lib.ITradePortal"
behaviorConfiguration="web"
/>
<endpoint address="Operations/" binding="wsDualHttpBinding"
contract="CFP_Web_Lib.ITradeOperations"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IPubSubService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
<mexHttpBinding>
<binding name="NewBinding0" />
</mexHttpBinding>
</bindings>
答案 0 :(得分:4)
默认的返回格式是XML,因此当您的操作返回String时,它将被格式化为XML - 字符串内容在元素内。返回任何的最简单方法是使用raw programming model。您的操作看起来像这样:
<WebGet(UriTemplate:="Reporting/GetClientTrades/{ClientID}")> _
<OperationContract()> _
Function GetClientTrades(ByVal ClientID As String) As Stream
实施:
Function GetClientTraces(ByVal ClientID As String) As Stream Implements ITradePortal.GetClientTraces
Dim result as String = "<tradeHistory>...</tradeHistory>"
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml" ' or anything you need
return new MemoryStream(Encoding.UTF8.GetBytes(result))
End Function
如果您不想处理Streams,另一个选项是将返回类型更改为XmlElement(或XElement)。这是“按原样”写出来的,因此您可以返回任意XML。
另一个选择是创建用于保存该数据的类。 TradeHistory类将包含对“Trade”实例的引用,而Trade类将具有使用该属性声明的许多字段。然后该操作的返回类型为TradeHistory
。