XML 序列化 - 客户端缺少命名空间前缀 [英] XML Serialization - Missing Namespace Prefix at client end
问题描述
我创建了一个 .NET Web 服务,它返回一个对象,比如getResponse"类.
I have created a .NET web service that returns an object, say Class "getResponse".
WS 返回以下响应...
The WS returns the following response ...
<getResponse xmlns="http://tempuri.org/getCustomer/wsdl/">
<Result xmlns="http://tempuri.org/getCustomer/">
<ResultCode>OK</ResultCode>
<ErrorsList/>
</Result>
</getResponse>
而客户端实际上正在等待以下...(注意mes-root:"前缀)
while the client is actually waiting for the following... (note the "mes-root:" prefix)
<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>
我怎样才能做到这一点?我是否需要在 getResponse 类上设置某些 XML 序列化属性,以便 mes-root 前缀显示在客户端?
How can I accomplish this ? Do i need to set certain XML Serialization atttibutes on the getResponse class for the mes-root prefix to show up at the client end ?
我在以下位置发现了类似的问题 http://forums.asp.净/t/1249049.aspx.老实说,我不太了解它,我无法让它工作.
I found a similar question at the following location http://forums.asp.net/t/1249049.aspx. To be honest, I do not quite understand it and I am unable to get it to work.
推荐答案
通常情况下,客户端必须符合 Web 服务发送的响应类型.但是,您的情况似乎有所不同,因为您似乎正在构建一个 Web 服务,为预先存在的客户端提供格式化的响应.
In usual conditions, it is the client which must conform to the type of response that a Web service sends. Your case, however, appears to be different, since you appear to be building a Webservice that provides a pre-existing client a formatted response.
解决命名空间前缀问题,你在问题中提到的链接提供了合适的解决方案;您需要在序列化过程中引导" XmlSerializer,您可以通过将 XmlNamespaceDeclarations
属性指定为返回 XmlSerializerNamespaces
类型的对象的属性来实现此目的.该属性也需要可设置,否则将不会应用命名空间.
To solve the namespace prefix problem, the link you mentioned in your question provides an appropriate solution; You will need to "guide" the XmlSerializer during the serialization process and you can do this by specifying the XmlNamespaceDeclarations
attribute to a property that returns an object of type XmlSerializerNamespaces
. The property will need to be settable as well, or the namespaces will not be applied.
当您将以下代码添加到 getResponse
类时,xml 响应将按照您示例中的预期格式:
When you add the following code to your getResponse
class, the xml response will as per expected format in your example:
[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.
}
}
分析为此类 Web 服务生成的 WSDL 类显示以下方法(GetMyResponse"是我给返回 GetResponse 对象的 WS 方法的名称):
Analzing the WSDL class generated for such a webservice reveals the following method ("GetMyResponse" is the name I gave to the WS method that returns a GetResponse object):
[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);
}
我相信 RequestNamespace
和 ResponseNamespace
属性会有所作为.
I believe that the RequestNamespace
and ResponseNamespace
attributes which make a difference.
希望这可以解决理解这里发生的底层 Xml 序列化的一些问题.
Hope this clears up a few issues in understanding the underlying Xml Serialization that's taking place here.
编辑(评论后)
这是我通过我的测试网络服务收到的回复:
Here is the response I received via my 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>
这篇关于XML 序列化 - 客户端缺少命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!