BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in WSCF/WCF

 
 
Page 3842 of 20224

Serializing an abstract data contract

Blogger : Geekswithblogs.net
All posts : All posts by Geekswithblogs.net
Category : WSCF/WCF
Blogged date : 2008 May 27

Recently I needed to serialize abstract classes in WCF.

The best way to explain it is with an example.

I have a abstract class

[KnownType(typeof(CreditCardPayment))]
public abstract class Payment
{
    private string _amount;
    private string _currency;
    [DataMember]
    public string currency
    {
        get { return _currency; }
        set { _currency = value; }
    }
    [DataMember]
    public string amount
    {
        get { return _amount; }
        set { _amount = value; }
    }
 
}

The class deriving it is

[DataContract]
public class CreditCardPayment:Payment
{
    private string _creditCardNumber;
    private DateTime _expiryDate;
    private string _securityCode;
    [DataMember]
    public string securityCode
    {
        get { return _securityCode; }
        set { _securityCode = value; }
    }
    [DataMember]
    public DateTime expiryDate
    {
        get { return _expiryDate; }
        set { _expiryDate = value; }
    }
 
    [DataMember]
    public string creditCardNumber
    {
        get { return _creditCardNumber; }
        set { _creditCardNumber = value; }
    }
 
}

When I attempted to serialize this to a message queue, I got the error below

at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write3_Payment(String n, String ns, Payment o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write5_AirTimeOrder(String n, String ns, AirTimeOrder o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write6_AirTimeOrder(Object o)


The solution is to the code below

[DataContract(Namespace = "www.pro.com")]
[KnownType(typeof(CreditCardPayment))]

[System.Xml.Serialization.XmlInclude(typeof(CreditCardPayment))]
public abstract class Payment
{
    private string _amount;
    private string _currency;
    [DataMember]
    public string currency
    {
        get { return _currency; }
        set { _currency = value; }
    }
-------------------------------------------------------------------------------


Read comments or post a reply to : Serializing an abstract data contract
Page 3842 of 20224

Newest posts
 

    Email TopXML