提供以字符串形式返回 XML 的 WebMethod。
我一直在阅读每个人都说当您想要返回 XML 时返回 XmlDocument。有没有办法以字符串形式返回原始 XML?我使用过许多 Web 服务(由其他人编写),它们返回包含 XML 的字符串。如果返回 XmlDocument,不在 .Net 上的用户如何使用该方法?
仅将原始 XML 作为字符串返回而不用
包装的方法是什么?
谢谢!
I keep reading how everyone states to return a XmlDocument when you want to return XML. Is there a way to return raw XML as a string? I have used many web services (written by others) that return a string which contains XML. If you return a XmlDocument how is that method consumed by users that are not on .Net?
What is the method to just return the raw XML as string without it having being wrapped with <string></string>
?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 .Net Web 服务,首先要了解的是它们使用 SOAP 协议。这意味着无论您通过 Web 方法返回什么类型,它们都将被序列化为 XML。因此,每个返回的对象都将是一个传递回调用者的 XML 字符串。
如果您仍然只是希望将 XML 作为实际字符串值返回,则在 Web 服务中创建一个服务器端方法,如下所示:
如果您尝试将实际 XML 返回给调用者,则只需让 .Net 负责将 XML 序列化为最后
,如果您只是寻找 XML 响应,而没有 SOAP 协议将响应包装和序列化为 XML,那么请尝试来自定制页面的页面响应:
The first thing to understand with .Net webservices are that they used the SOAP protocol. This means that whatever types you return via your web method they will be serialised to XML. Therefore, every returned object will be an XML string passed back to the caller.
If you are still simply looking to return XML as an actual string value then create a server side method within your webservice as follows:
If however you are trying to return actual XML to a caller then simply let .Net take care of serialising the XML as follows:
Lastly, if you are simply looking for a XML response without the SOAP protocol wrapping and serialising the response as XML then try a page response from a bespoke page: