更改WebService方法结果名称
这可能是一件简单的事情,但我还没有找到可行的方法。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,使用
XmlElementAttribute
将允许您重新定义序列化元素的名称。然而,在 ASMX Web 服务中,这似乎不起作用。但是,通过使用 WebMethod 上的属性,我能够产生您正在寻找的行为。试试这个: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:您需要添加 XmlRoot 声明,如下所示:
What you need is to add the XmlRoot declaration like this:
我解决了类似的问题,我修改了 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