提供以字符串形式返回 XML 的 WebMethod。

发布于 2024-08-25 17:33:33 字数 221 浏览 4 评论 0原文

我一直在阅读每个人都说当您想要返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-09-01 17:33:33

对于 .Net Web 服务,首先要了解的是它们使用 SOAP 协议。这意味着无论您通过 Web 方法返回什么类型,它们都将被序列化为 XML。因此,每个返回的对象都将是一个传递回调用者的 XML 字符串。

如果您仍然只是希望将 XML 作为实际字符串值返回,则在 Web 服务中创建一个服务器端方法,如下所示:

[WebMethod]
public string ReturnXMLString() {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("<root><item>Hello World</item></root>");
    return xmlDoc.OuterXML;
}

如果您尝试将实际 XML 返回给调用者,则只需让 .Net 负责将 XML 序列化为最后

[WebMethod]
public XmlDocument ReturnXMLString() {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("<root><item>Hello World</item></root>");
    return xmlDoc;
}

,如果您只是寻找 XML 响应,而没有 SOAP 协议将响应包装和序列化为 XML,那么请尝试来自定制页面的页面响应:

void Page_Load(object sender, EventArgs e) {
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.LoadXml("<root>Hello World</root>");
Response.ContentType ="text/xml";
xmlDoc.Save(Response.Output);

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:

[WebMethod]
public string ReturnXMLString() {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("<root><item>Hello World</item></root>");
    return xmlDoc.OuterXML;
}

If however you are trying to return actual XML to a caller then simply let .Net take care of serialising the XML as follows:

[WebMethod]
public XmlDocument ReturnXMLString() {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml("<root><item>Hello World</item></root>");
    return xmlDoc;
}

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:

void Page_Load(object sender, EventArgs e) {
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.LoadXml("<root>Hello World</root>");
Response.ContentType ="text/xml";
xmlDoc.Save(Response.Output);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文