2017-08-02 82 views
0

我有我想添加額外屬性的WSDL服務類。當我試圖反序列化我的派生類時,它給出錯誤「您需要將XmlChoiceIdentifierAttribute添加到'ObjCreatePaperClipTransaction'成員。」使用XmlChoiceIdentifierAttribute序列化派生類成員

這是我在服務類上寫的代碼。 (executeCreatePaperClipTransaction & CreatePaperClipTransactionType是從代理對象類)

namespace MyProject.DTO 
{ 
    [XmlType("executeCreatePaperClipTransaction")] 
    public partial class CustomExecuteCreatePaperClipTransaction : executeCreatePaperClipTransaction 
    { 
     [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)] 
     [XmlElement("CreatePaperClipTransaction")] 
     public CustomCreatePaperClipTransactionType ObjCreatePaperClipTransaction { get; set; }   
    }  

    public partial class CustomCreatePaperClipTransactionType : CreatePaperClipTransactionType 
    { 
     [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)] 
     public executeCreateLoanIncrease ObjLoanIncreaseRequest { get; set; } 

     [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)] 
     public executeCreateFreeFormEventFeePayment ObjFreeFormEventFeePaymentRequest { get; set; } 
     } 
    } 

當我刪除[XmlElement("CreatePaperClipTransaction")]線,其工作的罰款。但在seralized XML我想標籤名稱是CreatePaperClipTransaction而不是ObjCreatePaperClipTransaction

我通過這個答案去了,但我不知道我怎麼能在我的情況下實現https://stackoverflow.com/a/20379038/1169180

+0

爲什麼你有兩個'[XmlElement]'屬性附加到'ObjCreatePaperClipTransaction'屬性?你的問題是XML中可能有兩個不同的元素名稱,即和'',並且你想將它們綁定到ObjCreatePaperClipTransaction屬性?此外,由於缺少基本類型,您的代碼無法編譯。你可以添加它們以及要反序列化的XML樣本,因此將你的問題擴展到[mcve]? – dbc

回答

0

而不是增加兩個獨立[XmlElement]屬性您ObjCreatePaperClipTransaction財產,你應該添加一個單獨的屬性與所有必要的信息:

[XmlType("executeCreatePaperClipTransaction")] 
public partial class CustomExecuteCreatePaperClipTransaction : executeCreatePaperClipTransaction 
{ 
    [XmlElement(ElementName = "CreatePaperClipTransaction", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)] 
    public CustomCreatePaperClipTransactionType ObjCreatePaperClipTransaction { get; set; }   
}  

工作.Net fiddle

當您添加多個[XmlElement]屬性的屬性,你通知XmlSerializer多個不同的XML元素名稱應綁定到相同的屬性,例如由於屬性值是多態的:

public class BaseClass 
{ 
} 

public class DerivedClass : BaseClass 
{ 
} 

public class RootObject 
{ 
    [XmlElement(ElementName = "BaseClassProperty", Type = typeof(BaseClass))] 
    [XmlElement(ElementName = "DerivedClassProperty", Type = typeof(DerivedClass))] 
    public BaseClass Property { get; set; } 
} 

在上面的例子中,如果Property被分配一個DerivedClass值,下面的XML將被生成:

<RootObject> 
    <DerivedClassProperty /> 
</RootObject> 

這似乎並不適用於此。您只想將您的屬性綁定到XML元素名稱<CreatePaperClipTransaction>