首页 文章

在WebService asmx中格式化SOAP消息

提问于
浏览
0

我在使用SOAP 1.1消息的Web服务中遇到了客户端所需格式的问题,这正是他所期望的:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
    <SOAP-ENV:Body >
        <m:PingResponse xmlns:m="http://www.mycompany.com/service" Token="E30ED3AA-65DE-48F9-BEA4-BA021B119625" Echo="Hello!" Status="Successful" />
    </SOAP-ENV:Body >
</SOAP-ENV:Envelope >

这就是我现在所拥有的

<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>
        <PingRequestResponse xmlns="http://www.mycompany.com/service">
            <m:PingResponse Token="E30ED3AA-65DE-48F9-BEA4-BA021B119625" Status="Success" xmlns:m="http://www.mycompany.com/service" />
        </PingRequestResponse>
    </soap:Body>
</soap:Envelope>

这就是我的实施方式

[WebService(Namespace = "http://www.mycompany.com/service")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public PingResponse PingRequest([XmlAttribute] string Token)
    {
        //do stuffs
        if (success) {
            return new PingResponse { Token = Token, Status = "Success" };
        }
        else {
            return new PingResponse { Token = Token, Status = "Failed", Error = new Error { Code = "NoConnection", Message = "Can't reach the server" } };
        }
    }
}

[XmlRoot(ElementName = "PingResponse", Namespace = "http://www.mycompany.com/service")]
public class PingResponse
{
    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();

    [XmlAttribute]
    public string Token { get; set; }
    [XmlAttribute]
    public string Status { get; set; }
    [XmlElement(ElementName = "Error", Namespace = "http://www.mycompany.com/service")]
    public Error Error { get; set; }

    public PingResponse()
    {
        xmlns.Add("m", "http://www.mycompany.com/service");
    }
}

[DataContract(Namespace = "http://www.mycompany.com/service")]
public class Error
{
    [XmlAttribute]
    public string Code { get; set; }
    [XmlAttribute]
    public string Message { get; set; }
}

因此,您可以在我的响应中看到一个名为 PingRequestResponse 的aditional Xml元素,由WebService使用MethodNameResponse格式生成,之前我还有另一个名为 PingRequestResult 的元素,位于PingRequestResponse下面,但我能够在 class 中使用 [XmlRoot(ElementName = "PingResponse", 删除它

所以我需要做下一个:

  • 删除PingRequestResponse元素并将PingResponse直接放在正文下方

  • 将SOAP消息(信封和正文)的名称空间更改为"SOAP-ENV"

  • 从SOAP消息中删除xsi和xsd

任何帮助将不胜感激,谢谢 .

EDIT:

到目前为止,我还没有WSDL,所以现在这不是一个选择

1 回答

  • 0

    我花了很多时间在网络库中添加xml命名空间 . 当其他方法不起作用时,我只需解析我需要的字符串 . 更清洁

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    
    namespace ConsoleApplication13
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                string xmlStr =
                    "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" >" +
                       "<SOAP-ENV:Body >" +
                          "<m:PingResponse xmlns:m=\"http://www.derbysoft.com/chapaai\"  />" +
                       "</SOAP-ENV:Body >" +
                    "</SOAP-ENV:Envelope >";
    
                XElement envelope = XElement.Parse(xmlStr);
                XElement response = envelope.Descendants().Where(x => x.Name.LocalName == "PingResponse").FirstOrDefault();
    
                response.Add(new XAttribute("Token", "E30ED3AA-65DE-48F9-BEA4-BA021B119625"));
                response.Add(new XAttribute("Echo", "Hello"));
                response.Add(new XAttribute("Status", "Successful"));
            }
    
        }
    
    }
    

相关问题