更改WebService方法结果名称

发布于 2024-11-03 14:56:00 字数 1369 浏览 3 评论 0原文

这可能是一件简单的事情,但我还没有找到可行的方法。

我有一个 C# Web 服务,当前有这样的输出:

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

我需要的是这样的输出:

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

我尝试将所有内容包装在“InformationResponse”对象中,但我仍然有“GetInformationResult”对象封装它。我基本上需要将“GetInformationResult”重命名为“InformationResponse”。

谢谢!

ETA:对象/方法信息

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

  return infoSummary;
}

InformationResponse 对象:

[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;
    }
  }
}

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;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2024-11-10 14:56:00

通常,使用 XmlElementAttribute 将允许您重新定义序列化元素的名称。然而,在 ASMX Web 服务中,这似乎不起作用。但是,通过使用 WebMethod 上的属性,我能够产生您正在寻找的行为。试试这个:

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

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)
{  
   ....
}
忆依然 2024-11-10 14:56:00

您需要添加 XmlRoot 声明,如下所示:

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

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

[XmlRoot("MyName")]
public class MyName
{}
魂归处 2024-11-10 14:56:00

我解决了类似的问题,我修改了 WSDL 并删除结果元素,然后使用 wsdl.exe 生成代理类并为我的 ASMX 使用代理类

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文