从Web服务响应中删除XML标记 [英] Removing XML tags from a webservice response

查看:94
本文介绍了从Web服务响应中删除XML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于网络服务的2个文件。它会返回一些xml。

Service1.asmx:
<% @ WebService 语言 < font color ="#0000ff"size = 2> =" C#" CodeBehind =" Service1.asmx.cs" Class =" WebService.Service1" %>

Service1.asmx.cs
namespace < font size = 2> WebService
{
[WebService(Namespace = http://tempuri.org/ )] < br> [ WebServiceBinding (ConformsTo = WsiProfiles .BasicProfile1_1)]
[
ToolboxItem false )]
public class Service1 :System.Web。服务。 WebService
{
[
WebMethod ]
< font style ="font-size:12px"> public string GetThumbnail()
{
string xmlResponse = "< StatusText>" +" Status1" + "< / StatusText>" +
"< ExtraText>" +" Extra" + < / ExtraText ;


return xmlResponse;
}
}
}


响应的SOAP是:
--------------------
< soap:Envelope xmlns:soap ="
http://schemas.xmlsoap.org/soap/envelope/ " xmlns:xsi =" http:// www。 w3.org/2001/XMLSchema-instance " xmlns:xsd =" http://www.w3。 org / 2001 / XMLSchema ">
< soap:Body>
< GetThumbnailResponse xmlns ="
http://tempuri.org/ ">
< GetThumbnailResult>
<![CDATA [
< StatusText> Status1< / StatusText>

< ExtraText> Extra< / ExtraText> ;]]>
< / GetThumbnailResult>
< / GetThumbnailResponse>
< / soap:Body>
< / soap:Envelope>
- --------------------

我的问题:有没有办法删除开始和结束< GetThumbnailResult>标签和CDATA标签,同时仍然保持< StatusText>和< ExtraText>标签和他们的数据?

谢谢,
〜杰夫


解决方案

第一件事:你永远不应该像对待文本那样对待XML。当特殊字符没有被正确编码时,这将给你带来麻烦。

第二,与第一个相关:如果你要返回XML,那么你应该返回XML。您可以创建并返回XmlDocument的根。

第三,您自己的XML本身并不是一个格式良好的文档。它在根级别有两个元素。我很确定你甚至无法将其加载到XmlDocument中。

您可以使用SoapDocumentAttribute或其他一个来摆脱< GetThumbnailResult>。

Hi, Here are the 2 files I use for my webservice. It returns some xml.

Service1.asmx:
<%@ WebService Language="C#" CodeBehind="Service1.asmx.cs" Class="WebService.Service1" %>

Service1.asmx.cs
namespace WebService
{
[WebService(Namespace = http://tempuri.org/)]
[
WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[
ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
    [
WebMethod
    public string GetThumbnail()
    {
        string xmlResponse = "<StatusText>" + "Status1" + "</StatusText>" + 
                                           "<ExtraText>" + "Extra" + </ExtraText;

        return xmlResponse;
    }
}
}


The SOAP for the response is:
--------------------
<soap:Envelope xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetThumbnailResponse xmlns="
http://tempuri.org/">
         <GetThumbnailResult>
            <![CDATA[
                <StatusText>Status1</StatusText>
                <ExtraText>Extra</ExtraText>]]>
        </GetThumbnailResult>
      </GetThumbnailResponse>
   </soap:Body>
</soap:Envelope>
----------------------

My questions: Is there a way to remove the opening and closing <GetThumbnailResult> tags and the CDATA tag, while still keeping the <StatusText> and <ExtraText> tags and their data?

Thanks,
~Jeff


解决方案

First thing: you should NEVER treat XML like it's text. That will give you nothing but trouble when special characters aren't properly encoded.

Second, and related to the first: if you're going to return XML, then you should return XML. You can create and return the root of an XmlDocument.

Thirdly, by itself, your XML is not a well-formed document. It has two elements at root level. I'm pretty sure you can't even load that into an XmlDocument.

You may be able to fiddle around with the SoapDocumentAttribute or one of the others to get rid of the <GetThumbnailResult>.


这篇关于从Web服务响应中删除XML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆