2009-12-18 53 views
1

我有一個使用xsd.exe工具創建的對象,該工具在代碼中定義了xml屬性,但來自我的web服務的SOAP響應正在返回xmlelements而不是屬性。WCF:SOAP輸出主體包含標記名稱而不是元素

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class Accountinfo { 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string UpdatedDate; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string UpdatedBy; 

... etc 

正如你所看到的,UpdatedDate等被定義爲屬性。 當我打電話給我的服務,皁體,我回去返回Accountinfo元素,例如:

<a:Accountinfo> <a:UpdatedBy>IGD</a:UpdatedBy> <a:UpdatedDate>12/18/2009 9:43:06 AM</a:UpdatedDate> ...等

我在尋找什麼是<AccountInfo UpdatedBy="IGD" UpdatedDate="12/18/2009 9:43:06 AM" ... />

我沒有太多有關XML,SOAP或WCF的經驗,但我現在正在使用這三者,並且需要這些工作。我在這裏錯過了什麼?

回答

2

您的WCF SOAP webservice是否使用標準WCF DataContractSerializer?如果是這樣 - 那是預期的行爲。爲了提高約10%的速度,DataContractSerializer(在WCF中默認使用,除非您明確要求XmlSerializer)放棄XML屬性並將所有內容作爲元素序列化。

即使您的數據具有所有這些[XmlAttribute]屬性也沒有什麼區別 - 您需要特別要求XmlSerializer,無論是在創建代理客戶端時(使用svcutil.exe還是Visual Studio的「添加服務引用」對話框),或者通過在您的服務實現上指定[XmlSerializerFormat]屬性。

瞭解更多關於XmlSerializerFormat attribute看丹Rigsby的的WCF DataContractSerializer vs. XmlSerializer

比較優秀
相關問題