当前位置: 代码迷 >> Web Service >> WCF中,string内容的xml如何才能作为soap消息body中的result返回
  详细解决方案

WCF中,string内容的xml如何才能作为soap消息body中的result返回

热度:86   发布时间:2016-05-02 02:16:34.0
WCF中,string内容的xml怎么才能作为soap消息body中的result返回?
本帖最后由 t_eel 于 2015-04-08 22:35:52 编辑
以下是我自己尝试的方案:

方案一:直接返回string
public string GetData()
        {
            string content = "<data></data>";
            return content;
        }

得到的结果是:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetDataResponse xmlns="http://tempuri.org/">
         <GetDataResult>&lt;data>&lt;/data></GetDataResult>
      </GetDataResponse>
   </s:Body>
</s:Envelope>


方案二:返回Stream
public Stream GetData()
        {
            string content = "<data></data>";
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content));
            return ms;
        }

得到的结果是:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetDataResponse xmlns="http://tempuri.org/">
         <GetDataResult>PGRhdGE+PC9kYXRhPg==</GetDataResult>
      </GetDataResponse>
   </s:Body>
</s:Envelope>

ps:直接返回Stream在REST的方式中是可以的,但soap却不行,哪位晓得原因请指教

Please help!
------解决思路----------------------
想作为soap消息的一部分xml返回是不可能的,除非你定义成一个实体,序列化工作由wcf完成
另外就算这样传递了,客户端收到的时候也可以转回xml
------解决思路----------------------
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetDataResponse xmlns="http://tempuri.org/">
         <GetDataResult>&lt;data>&lt;/data></GetDataResult>
      </GetDataResponse>
   </s:Body>
</s:Envelope>

这个没有问题吧,转义了而以。
  相关解决方案