开发者

Changing the WebService method result name

开发者 https://www.devze.com 2023-03-01 08:43 出处:网络
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:

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<P开发者_运维百科olicy>
{
  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;
    }
  }
}


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)
{  
   ....
}


What you need is to add the XmlRoot declaration like this:

[XmlRoot("MyName")]
public class MyName
{}


I resolved similar problem I modified WSDL and remove result element, then generate proxy class with wsdl.exe and use proxy class for my ASMX

0

精彩评论

暂无评论...
验证码 换一张
取 消