Webservice returning custom XML
I'm dealing with a legacy web service and I'm running into trouble trying to adjust the methods' responses to previously defined XMLs. Sadly, changing the WSDL is out of question and to further complicate the issue, it's incompatible with WSDL.exe tool.
The oh!-so-wanted XML:
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-i开发者_C百科nstance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="my/Namespace">Success</Response>
</soap:Body>
</soap:Envelope>
Through experimentation - since I'm not fluent in .NET - I've reached the solution below, which would work if both methods used different XML tags. Unfortunately, when the ResponseElementName
is set to the same value it "breaks" the web service with the following error:
The XML element 'Response' from namespace 'my/Namespace' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute.
So, is there another way to achieve the XML nirvana without changing the whole system?
Thank you in advance!
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";
}
}
First of all, you should be using WCF for any new web service development.
Secondly, try returning the XmlElement type instead of string. Try building exactly the XML you want to return, then returning it.
精彩评论