Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialization - Missing Namespace Prefix at client end

I have created a .NET web service that returns an object, say Class "getResponse".

The WS returns the following response ...

<getResponse xmlns="http://tempuri.org/getCustomer/wsdl/">
   <Result xmlns="http://tempuri.org/getCustomer/">
      <ResultCode>OK</ResultCode>
      <ErrorsList/>
   </Result>  
</getResponse>

while the client is actually waiting for the following... (note the "mes-root:" prefix)

<mes-root:getResponse xsi:schemaLocation="http://tempuri.org/getCustomer/ getCustomer_V200906.xsd" xmlns:mes-root="http://tempuri.org/getCustomer/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <mes-root:Result>
  <mes-root:ResultCode/>
  <mes-root:ErrorsList/>
 </mes-root:Result>
</mes-root:getResponse>

How can I accomplish this ? Do i need to set certain XML Serialization atttibutes on the getResponse class for the mes-root prefix to show up at the client end ?

EDIT: I found a similar question at the following location http://forums.asp.net/t/1249049.aspx. To be honest, I do not quite understand it and I am unable to get it to work.

like image 679
Preets Avatar asked Dec 22 '22 12:12

Preets


2 Answers

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>
like image 75
Cerebrus Avatar answered Jan 19 '23 20:01

Cerebrus


OK, the second sample is now valid XML, but it isn't semantically equivalent to the first sample, because the first one has two namespaces and the second one only has one.

Is the problem that you're sending XML that isn't semantically correct, or that they specifically require the prefix to be called mes-root?

like image 21
Greg Beech Avatar answered Jan 19 '23 21:01

Greg Beech