我在使用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; }
}
因此,正如您在我的响应中看到的那样,WebService以MethodNameResponse格式生成了一个名为PingRequestResponse的可选Xml元素,之前我在PingRequestResponse下面还有另一个名称为PingRequestResult,但是我能够用类中的[XmlRoot(ElementName="PingResponse",删除它
所以我需要做下一步:
- 删除PingRequestResponse元素,并将PingResponse直接放在主体下方
- 将SOAP消息的命名空间(信封和正文)更改为"SOAP-ENV">
- 从SOAP消息中删除xsi和xsd
如有任何帮助,我们将不胜感激,谢谢。
编辑:
到目前为止,我还没有WSDL,所以这不是的一个选项
我过去花了很多时间与添加xml名称空间的Net库作斗争。当其他方法不起作用时,我只需解析我需要的字符串。更清洁的
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"));
}
}
}