Question Using IgnoreDataMemberAttribute for array types in a DataContract


Well-known member
Mar 16, 2011
Programming Experience
I have a class that I need to serialize using XmlSerializer. The class is declared as follows:

<DataContract()> Public Class Recipient
<DataMember()> Public Property ID() As Byte
<DataMember()> Public Property Name() As String
<IgnoreDataMember()> Public ReadOnly Property Gifts() As Gift()
'Return value
End Get
End Property
End Class

When I serialize the class, it serializes with no errors, but it serializes the Gifts property even though I have the property marked with the IgnoreDataMemberAttribute. On the following page:

Using Data Contracts

In the Notes section, it says that the IgnoreDataMemberAttribute is ignored for types marked with the DataContractAttribute, which my Gift class is. I can't remove the DataContractAttribute from my Gift class, because I need it there, but I also need the Gifts property in my Recipient class. Is there any workaround to prevent the Gifts property from getting serialized? Thanks.
Top Bottom