Pre*_*ets 4 xml serialization web-services xml-serialization
我创建了一个.NET Web服务,它返回一个对象,比如Class"getResponse".
WS返回以下响应......
<getResponse xmlns="http://tempuri.org/getCustomer/wsdl/">
<Result xmlns="http://tempuri.org/getCustomer/">
<ResultCode>OK</ResultCode>
<ErrorsList/>
</Result>
</getResponse>
Run Code Online (Sandbox Code Playgroud)
而客户端实际上正在等待以下...(注意"mes-root:"前缀)
<mes-root:getResponse xsi:schemaLocation="http://tempuri.org/getCustomer/ getCustomer_V200906.xsd" xmlns:mes-root="http://tempuri.org/getCustomer/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<mes-root:Result>
<mes-root:ResultCode/>
<mes-root:ErrorsList/>
</mes-root:Result>
</mes-root:getResponse>
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?我是否需要在getResponse类上设置某些XML Serialization atttibutes,以便在客户端显示mes-root前缀?
编辑:我在以下位置找到了类似的问题:http://forums.asp.net/t/1249049.aspx.说实话,我不太了解它,我无法让它工作.
Cer*_*rus 10
在通常情况下,客户端必须符合Web服务发送的响应类型.但是,您的情况似乎有所不同,因为您似乎正在构建一个Web服务,为预先存在的客户端提供格式化的响应.
要解决名称空间前缀问题,您在问题中提到的链接提供了适当的解决方案; 您需要在序列化过程中"引导"XmlSerializer,并且可以通过XmlNamespaceDeclarations为返回类型对象的属性指定属性来执行此操作XmlSerializerNamespaces.该属性也需要设置,否则将不会应用命名空间.
将以下代码添加到getResponse类中时,xml响应将按照示例中的预期格式进行:
[XmlNamespaceDeclarations()]
public XmlSerializerNamespaces xmlsn
{
get
{
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add("mes-root", "http://tempuri.org/getCustomer/");
return xsn;
}
set
{
//Just provide an empty setter.
}
}
Run Code Online (Sandbox Code Playgroud)
为这样的Web服务生成的WSDL类的分析揭示了以下方法("GetMyResponse"是我给WSs方法返回GetResponse对象的名称):
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/getCustomer/GetMyResponse", RequestNamespace = "http://tempuri.org/getCustomer/", ResponseNamespace = "http://tempuri.org/getCustomer/", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public GetResponse GetMyResponse()
{
object[] results = this.Invoke("GetMyResponse", new object[-1 + 1]);
return (GetResponse)results(0);
}
Run Code Online (Sandbox Code Playgroud)
我相信这些RequestNamespace和ResponseNamespace属性有所不同.
希望这能解决一些问题,理解这里发生的基础Xml序列化.
编辑(评论后)
以下是我通过Test Webservice收到的回复:
<?xml version="1.0" encoding="utf-8"?>
<mes-root:GetResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mes-root="http://tempuri.org/getCustomer/">
<mes-root:Result>OK</mes-root:Result>
<mes-root:ErrorsList>
<mes-root:string>SomeErrors</mes-root:string>
<mes-root:string>SomeMoreErrors</mes-root:string>
</mes-root:ErrorsList>
</mes-root:GetResponse>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8547 次 |
| 最近记录: |