Hi Preets,
In usual conditions, it is the client which must conform to the type of response that a Web service sends. Your case, however, appears to be different, since you appear to be building a Webservice that provides a pre-existing client a formatted response.
To solve the namespace prefix problem, the link you mentioned in your question provides an appropriate solution; You will need to "guide" the XmlSerializer during the serialization process and you can do this by specifying the XmlNamespaceDeclarations
attribute to a property that returns an object of type XmlSerializerNamespaces
. The property will need to be settable as well, or the namespaces will not be applied.
When you add the following code to your getResponse
class, the xml response will as per expected format in your example:
[XmlNamespaceDeclarations()]
public XmlSerializerNamespaces xmlsn
{
get
{
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add("mes-root", "http://tempuri.org/getCustomer/");
return xsn;
}
set
{
//Just provide an empty setter.
}
}
Analzing the WSDL class generated for such a webservice reveals the following method ("GetMyResponse" is the name I gave to the WS method that returns a GetResponse object):
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/getCustomer/GetMyResponse", RequestNamespace = "http://tempuri.org/getCustomer/", ResponseNamespace = "http://tempuri.org/getCustomer/", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public GetResponse GetMyResponse()
{
object[] results = this.Invoke("GetMyResponse", new object[-1 + 1]);
return (GetResponse)results(0);
}
I believe that the RequestNamespace
and ResponseNamespace
attributes which make a difference.
Hope this clears up a few issues in understanding the underlying Xml Serialization that's taking place here.
Edit (after comments)
Here is the response I received via my Test webservice:
<?xml version="1.0" encoding="utf-8"?>
<mes-root:GetResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mes-root="http://tempuri.org/getCustomer/">
<mes-root:Result>OK</mes-root:Result>
<mes-root:ErrorsList>
<mes-root:string>SomeErrors</mes-root:string>
<mes-root:string>SomeMoreErrors</mes-root:string>
</mes-root:ErrorsList>
</mes-root:GetResponse>