C# 更改WebService方法结果名称

C# 更改WebService方法结果名称,c#,web-services,asmx,C#,Web Services,Asmx,这可能是一件容易的事情,但我还没有找到一个有效的方法来做 我有一个C#web服务,它当前的输出如下: <GetInformationResponse> <GetInformationResult> <Policy> </Policy> </GetInformationResult> <GetInformationResponse> <GetInformationResponse> &

这可能是一件容易的事情,但我还没有找到一个有效的方法来做

我有一个C#web服务,它当前的输出如下:

<GetInformationResponse>
  <GetInformationResult>
    <Policy>
    </Policy>
  </GetInformationResult>
<GetInformationResponse>
<GetInformationResponse>
  <InformationResponse>
    <Policy>
    </Policy>
  </InformationResponse>
<GetInformationResponse>
[XmlRoot("MyName")]
public class MyName
{}
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;
    }
  }
}
[System.Xml.Serialization.xmlclude(typeof(Policy))]
公共类信息响应:列表
{
私人政策领域;
/// 
[System.Xml.Serialization.xmlementAttribute(“Policy”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共政策
{
收到
{
返回此.policy字段;
}
设置
{
this.policyField=值;
}
}
}

通常,使用
XmlElementAttribute
可以重新定义序列化元素的名称。然而,在ASMXWeb服务中,这似乎不起作用。但是,通过使用WebMethod上的属性,我能够生成您要查找的行为。试试这个:

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

您需要的是添加如下XmlRoot声明:

<GetInformationResponse>
  <GetInformationResult>
    <Policy>
    </Policy>
  </GetInformationResult>
<GetInformationResponse>
<GetInformationResponse>
  <InformationResponse>
    <Policy>
    </Policy>
  </InformationResponse>
<GetInformationResponse>
[XmlRoot("MyName")]
public class MyName
{}

我解决了类似的问题,修改了WSDL并删除了结果元素,然后使用WSDL.exe生成代理类,并为我的ASMX使用代理类,您必须指定更多详细信息。它是SOAP服务吗?你为什么关心那些内部的东西。。。?Web服务是通过WSDL生成的还是通过代码生成的?我们能看到哪个定义了这个消息吗?它是一个SOAP服务,通过代码生成。就我个人而言,我并不关心内部结构,但管理层已将模式设置为一成不变的,它必须与上述内容相匹配。这是ASMX还是WCF服务?我尝试过,但结果是:
如果我封装它,结果是这样的: