coder
coder

Reputation: 4313

How to change the element name in .asmx response?

The webservice returns xml like below. The consuming client is expecting < MyMethodResult > to be < Response >.

Code:

[WebMethod]
public MyResponseObj MyMethod(MyRequestObj Request)
{
    try
    {
        MyResponseObj response = new MyResponseObj();
        response.Message = "test";
        response.Status = Status.success;
        return response;
    }
    catch(Exception ex)
    {
        throw;
    }
}

Actual Response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <MyMethodResponse xmlns="MyNameSpace">
      <MyMethodResult>
        <Status>success or failure</Status>
        <Message>string</Message>
      </MyMethodResult>
    </MyMethodResponse>
  </soap:Body>
</soap:Envelope>

Expected Response

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <MyMethodResponse xmlns="MyNameSpace">
      **<Response>**
        <Status>success or failure</Status>
        <Message>string</Message>
      **</Response>**
    </MyMethodResponse>
  </soap:Body>
</soap:Envelope>

Upvotes: 3

Views: 1611

Answers (1)

coder
coder

Reputation: 4313

I got it to work by decorating the MyResponseObj class with [XmlRoot("Response")]

[XmlRoot("Response")]
public partial class MyResponseObj
{

}

Upvotes: 2

Related Questions