Search code examples
c#web-servicesasmx

Changing the WebService method result name


This is probably an easy thing, but I haven't found a working way of doing it.

I have a C# web service which currently has an output like this:

<GetInformationResponse>
  <GetInformationResult>
    <Policy>
    </Policy>
  </GetInformationResult>
<GetInformationResponse>

What I need is output like this:

<GetInformationResponse>
  <InformationResponse>
    <Policy>
    </Policy>
  </InformationResponse>
<GetInformationResponse>

I've tried wrapping everything in an "InformationResponse" object, but I still have the "GetInformationResult" object encapsulating it. I basically need to rename "GetInformationResult" to "InformationResponse".

Thanks!

ETA: Object/method information

[WebMethod]
public InformationResponse GetInformation(GetInformationRequest GetInformationRequest)
{
  InformationResponse infoSummary = new InformationResponse();
  Policy policy = new Policy();
  // setting values

  return infoSummary;
}

InformationResponse object:

[System.Xml.Serialization.XmlInclude(typeof(Policy))]
public class InformationResponse : List<Policy>
{
  private Policy policyField;

  /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute("Policy", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
  public Policy Policy
  {
    get
    {
        return this.policyField;
    }
    set
    {
        this.policyField = value;
    }
  }
}

Solution

  • Usually, using an XmlElementAttribute will allow you to redefine the name of a serialized element. However, in an ASMX web service, this doesn't seem to work. However, by using an attribute on the WebMethod I was able to produce the behavior you're looking for. Try this:

    [WebMethod]
    [return: System.Xml.Serialization.XmlElementAttribute("InformationResponse")]
    public InformationResponse GetInformation(GetInformationRequest GetInformationRequest)
    {  
       ....
    }