C# 返回自定义XML的Web服务

C# 返回自定义XML的Web服务,c#,web-services,asmx,C#,Web Services,Asmx,我正在处理一个遗留的web服务,在尝试调整方法对以前定义的XML的响应时遇到了麻烦。遗憾的是,更改WSDL是不可能的,而且使问题进一步复杂化的是,它与WSDL.exe工具不兼容 哦-因此需要XML: <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://

我正在处理一个遗留的web服务,在尝试调整方法对以前定义的XML的响应时遇到了麻烦。遗憾的是,更改WSDL是不可能的,而且使问题进一步复杂化的是,它与WSDL.exe工具不兼容

哦-因此需要XML:

<soap:Envelope 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <Response xmlns="my/Namespace">Success</Response>
    </soap:Body>
</soap:Envelope>

首先,您应该将WCF用于任何新的web服务开发

其次,尝试返回XmlElement类型而不是字符串。尝试准确地构建要返回的XML,然后返回它

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;

[WebService(Name = "WSOcean",
    Namespace = "my/Namespace"
)]
public class WSOcean : System.Web.Services.WebService
{
    [WebMethod]
    [SoapDocumentMethod(
        Action = "Method1",
        RequestNamespace = "my/Method1/Namespace",
        ResponseElementName = "Response"
    )]
    [return: XmlText(DataType = "string")]
    public string Method1(int MessageType)
    {
        return "Example message 1";
    }

    [WebMethod]
    [SoapDocumentMethod(
        Action = "Method2",
        RequestNamespace = "my/Method2/Namespace",
        ResponseElementName = "Response"
    )]
    [return: XmlText(DataType = "string")]
    public string Method2(bool MessageStatus)
    {
        return "Random message 2";
    }
}