我正在写一个小型的SOAP网络服务(为了快乐而不是获得)而且我遇到了一个困扰我的小问题 - 即使它不是主要问题。
我有以下方法:
public class GetCustomerByCodeRequest
{
public string Code;
}
public class GetCustomerByCodeResponse
{
public Customer GetCustomerResult;
public string Status;
public string StatusDetail;
}
[WebMethod]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
[return: XmlElement("GetCustomerResponse")]
public GetCustomerResponse GetCustomer(GetCustomerRequest GetCustomer)
{
return null;
}
当我查看SOAP Request格式时,它看起来很完美:
<?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>
<GetCustomerByCode xmlns="http://tempuri.org/">
<Code>string</Code>
</GetCustomerByCode>
</soap:Body>
</soap:Envelope>
代码作为单个元素传递 - 正如我想要的那样。但是,对于我输入多个代码时返回多个客户的函数,我遇到了一个问题:
public class GetCustomersByCodeRequest
{
public string[] Codes;
}
public class GetCustomersByCodeResponse
{
public Customer[] GetCustomersResult;
public string Status;
public string StatusDetail;
}
[WebMethod]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
[return: XmlElement("GetCustomersByCodeResponse")]
public GetCustomersByCodeResponse GetCustomersByCode(GetCustomersByCodeRequest GetCustomersByCode)
{
return null;
}
当我查看SOAP请求格式时,我会看到以下内容:
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCustomersByCode xmlns="http://tempuri.org/">
<Codes>
<string>string</string>
<string>string</string>
</Codes>
</GetCustomersByCode>
</soap:Body>
</soap:Envelope>
令我烦恼的是定义代码时的部分 - 我不希望元素名称是&#39; string&#39;。我可以看到它为什么这样做 - 我的字符串数组 - 但我宁愿他们看起来像下面这样:
<Codes>
<code>string</code>
<code>string</code>
</Codes>
这可能吗?我非常生疏,并尝试了各种各样但通常最终会让它看起来更糟糕 - 各种各样的嵌套废话。
答案 0 :(得分:1)
您需要XmlArrayItem(ElementName =&#39;&#39;)属性。它会将数组中类型名称的默认使用转换为您想要的任何内容。
public class GetCustomersByCodeRequest
{
[XmlArrayItem(ElementName= "Code")]
public string[] Codes;
}