首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用WCF返回普通的旧XML

使用WCF返回普通的旧XML
EN

Stack Overflow用户
提问于 2011-10-22 17:52:03
回答 2查看 1K关注 0票数 2

这怎么会如此困难?为什么?返回简单的XML,不带名称空格和XML降序。使用XML Writer创建XML很容易,如果我把它输出到一个文件中,那就更好了。您究竟如何通过WCF返回内容。使用XML元素并不好,因为您松散了XML声明;使用字符串也不好,因为输出被包装在元素中。无法返回XML文档,因为它无法序列化。

我知道这个网站上有很多帖子,但没有一个人回答这个问题。我正在使用VB.NET (天哪,我真希望我有时间学习C#),即使使用IXmlSerializer,我也不能让数据契约工作。我需要通过WCF服务发回的输出示例如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
 <BookingResponse timestamp="" success="1">
<confirmation id="" track_code="" status="" notes="" tracking_url="" confirmed_at=""/>
 </BookingResponse>

不幸的是,我要发送到的服务不会接受除了这个纯XML之外的任何内容。不会接受额外的名称空间,这些名称空间似乎是WCF自己放入的。

我使用的是适用于Visual Studio2010的VB在线Rest模板。

帮助!请!总得有人有答案吧?

下面是一些代码:

代码语言:javascript
复制
<WebInvoke(UriTemplate:="BookJob", Method:="POST")>
        Public Function Create(ByVal XMLBooking As Stream) As Stream
            'DO SOME PROCESSING ON THE REQUEST - THIS ALL WORKS FINE.....

            Dim str As String

'This CreateResponseXML function creates a String version of the XML, which is built using XMLWriter.


       str = CreateResponseXML(True, vConfirmationID, sqlFuncs.BookingStatus("Confirmed"), "", "")

            Dim memoryStream As New MemoryStream()
            Using streamWriter As New StreamWriter(memoryStream)
                streamWriter.Write(str)
                streamWriter.Flush()

                memoryStream.Position = 0

                Return memoryStream
            End Using

        End Function

要添加到hassel,它们必须使用安全连接。我正在使用VB的在线Rest模板,该模板可用于Visual Studio2010,与我随处可见的普通模板相比,它大大减少了web.config中的条目。它看起来是这样的:

代码语言:javascript
复制
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"/>
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

    <bindings>
      <webHttpBinding>
        <binding transferMode="Streamed">
          <security mode="Transport" />
        </binding>
      </webHttpBinding>
    </bindings>

    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.vb file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true">
          <security mode="Transport" />
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>

  </system.serviceModel>



</configuration>

在这段代码之前,我尝试返回一个MemoryStream,但是他们得到的响应被包装在一个MemoryStream元素中,并且令人费解(但这可能是因为我在web.config中省略了transferMode="Streamed“条目。此版本根本不返回任何内容。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2011-10-22 17:58:54

使用WCFRestContrib,它有POX格式化程序(Plain Old Xml),这是干净的,没有任何名称空间。

xml格式化程序使用System.Runtime.Serialization.DataContractSerializer,,但与WCF REST Contrib Xml格式化程序(在

格式化程序概述中讨论)不同,POX格式化程序不序列化数据合约命名空间或xml schema属性,它不需要在xml中指定要反序列化的命名空间,也不要求元素按特定顺序排列。这使您能够提供和接受非常简单的xml。

请参阅POX Formatter Overview

票数 1
EN

Stack Overflow用户

发布于 2011-10-22 21:07:32

使用Stream作为返回值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7858694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档