【发布时间】:2011-03-02 12:25:49
【问题描述】:
为了符合客户端架构,我一直在尝试生成一个 WCF 客户端代理,该代理能够向下序列化为具有如下所示根节点的结构:
<quote:request
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:quote="https://example.com/services/schema/1.2/car_quote">
经过一番阅读,我很幸运地通过使用 XmlNameSpaceDeclarations 和 XmlSerializerNamespaces 更新了代理以包含所需的“引用”命名空间
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class request
{
[XmlNamespaceDeclarations()]
public XmlSerializerNamespaces xmlsn
{
get
{
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add("quote", "https://example.com/services/schema/1.2/car_quote");
return xsn;
}
set
{
//Just provide an empty setter.
}
}
...
提供:
<request
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:quote="https://example.com/services/schema/1.2/car_quote">
但是我对如何生成 quote:request 元素感到困惑。
环境:ASP.NET 3.5
【问题讨论】:
-
您的客户架构实际上不应该关心前缀是“quote”还是“etouq”,或者介于两者之间的任何东西。重要的是命名空间,而不是前缀,它只是命名空间的别名。任何真正关心使用哪个前缀的代码都应该立即修复,或者至少公开嘲笑它违反了基本的 XML 标准。
-
另一边:你的问题不是你没有生成前缀:你的问题是你的元素没有在正确的命名空间中生成。作为一个实验,您应该尝试修复命名空间问题,然后删除
[XmlNamespaceDeclarations]属性并查看客户端的代码是否仍然有效。
标签: c# wcf xml-serialization