2010-06-11 60 views
2

以符合客戶的模式,我一直在試圖產生一個能夠序列化到一個結構,看起來像下面這樣的根節點的WCF客戶端代理:強制WCF代理生成一個別名前綴

<quote:request 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:quote="https://foo.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://foo.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://foo.com/services/schema/1.2/car_quote"> 

但是我非常難以理解如何生成quote:request元素。

環境:ASP.NET 3.5

+1

您的客戶的模式實際上不應該在意是否前綴是「quote」還是「etouq」,或者其中的任何內容。這是重要的名稱空間,而不是前綴,它只是名稱空間的別名。任何真正關心使用哪個前綴的代碼應該立即修復,或者至少公開嘲笑違反基本XML標準。 – 2010-06-11 17:33:15

+0

另一方面:你的問題不是你沒有生成前綴:你的問題是你的元素沒有在正確的名稱空間中生成。作爲一個實驗,您應該嘗試修復名稱空間問題,然後移除'[XmlNamespaceDeclarations]'屬性並查看客戶端的代碼是否仍然有效。 – 2010-06-11 17:34:43

回答

1

我真的不能重現你的情況沒有一些WSDL來生成一個代理,但系列化位工作對我來說,如果我添加一個XmlRoot屬性。

using System.Xml.Serialization; 

[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[XmlRoot(Namespace="https://foo.com/services/schema/1.2/car_quote")] 
public partial class request 
{ 
    get 
    { 
     [XmlNamespaceDeclarations()] 
     public XmlSerializerNamespaces xmlsn 
     { 
      XmlSerializerNamespaces xsn = new XmlSerializerNamespaces(); 
      xsn.Add("quote", "https://foo.com/services/schema/1.2/car_quote"); 
      return xsn; 
     } 
    } 
    set { } 
}