我在使用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 的附加Xml元素,该元素由WebService以 MethodNameResponse 格式生成,之前我还有另一个名为 PingRequestResult 在PingRequestResponse下方,但我能够使用 [XmlRoot(ElementName =&#34; PingResponse&#34;,在我的班级
中删除它)所以我需要做下一个:
任何帮助将不胜感激,谢谢。
修改
到目前为止,我还没有WSDL,所以现在这不是一个选项
答案 0 :(得分:0)
我过去花了很多时间来争取添加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"));
}
}
}