2011-06-03 223 views
2

我想讓我的web服務用特定形式的XML進行回覆,我認爲只需將數據放入字符串並將其返回到網頁即可。從WCF服務返回原始XML

我試圖返回:

<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/">&lt;tradeHistory&gt;&lt;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" /&gt;&lt;tradeHistory&gt</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

你可以嘗試改變返回一個XmlElement而不是字符串的方法嗎?不知道我是否有這個WCF,雖然... – 2011-06-03 21:00:28

+0

以上的建議是一個很好的,不管它是否足以解決整個問題:如果你要返回XML,然後返回XML,而不是一個字符串。 – 2011-06-03 21:19:37

回答

3

默認返回格式是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 

另一種選擇,如果你不想來處理數據流,是返回類型更改爲XmlElement的(或的XElement)。這是寫「按原樣」,所以你可以返回任意的XML。

另一種選擇是創建類來保存該數據。 TradeHistory類將持有對「Trade」實例的引用,Trade類將具有許多使用該屬性聲明的字段。該操作然後將具有返回類型TradeHistory

+0

您必須從使用DataContractSerializer切換到XmlSerializer以便將'Trade'對象上的字段作爲屬性而不是子元素進行序列化。但是這樣做也會使得服務更加令人愉快,可以從可以再次反序列化的客戶端使用,而不必處理原始XML字符串。 – shambulator 2011-06-03 21:14:58

+0

這兩個例子都很完美。好答案! – evilfish 2013-06-20 12:49:40