如何更改.asmx响应中的元素名称?

时间:2014-05-19 15:48:37

标签: web-services asmx

webservice返回xml,如下所示。消费客户期待< MyMethodResult>要<回复>。

代码:

[WebMethod]
public MyResponseObj MyMethod(MyRequestObj Request)
{
    try
    {
        MyResponseObj response = new MyResponseObj();
        response.Message = "test";
        response.Status = Status.success;
        return response;
    }
    catch(Exception ex)
    {
        throw;
    }
}

实际回应:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <MyMethodResponse xmlns="MyNameSpace">
      <MyMethodResult>
        <Status>success or failure</Status>
        <Message>string</Message>
      </MyMethodResult>
    </MyMethodResponse>
  </soap:Body>
</soap:Envelope>

预期回应

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <MyMethodResponse xmlns="MyNameSpace">
      **<Response>**
        <Status>success or failure</Status>
        <Message>string</Message>
      **</Response>**
    </MyMethodResponse>
  </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:2)

我通过使用[XmlRoot("Response")]装饰MyResponseObj类来实现它的工作

[XmlRoot("Response")]
public partial class MyResponseObj
{

}