146

I am using .NET 3.5SP1 and DataContractSerializer to serialize a class. In SP1, they changed the behavior so that you don't have to include DataContract/DataMember attributes on the class and it will just serialize the entire thing. This is the behavior I am using, but now I need to ignore one property from the serializer. I know that one way to do this is to add the DataContract attribute to the class, and just put the DataMember attribute on all of the members that I want to include. I have reasons, though, that this will not work for me.

So my question is, is there an attribute or something I can use to make the DataContractSerializer ignore a property?

Neuron
  • 5,141
  • 5
  • 38
  • 59
NotDan
  • 31,709
  • 36
  • 116
  • 156

5 Answers5

211

Additionally, DataContractSerializer will serialize items marked as [Serializable] and will also serialize unmarked types in .NET 3.5 SP1 and later, to allow support for serializing anonymous types.

So, it depends on how you've decorated your class as to how to keep a member from serializing:

  • If you used [DataContract], then remove the [DataMember] for the property.
  • If you used [Serializable], then add [NonSerialized] in front of the field for the property.
  • If you haven't decorated your class, then you should add [IgnoreDataMember] to the property.
Doug
  • 5,208
  • 3
  • 29
  • 33
  • 2
    what did you mean by "decorated your class" pls explain ? – alamin Nov 01 '15 at 04:05
  • If you haven't used any attributes on your class, like [DataContract] or [Serializable], then you would add [IgnoreDataMember] to the property you don't want to serialize. See Paul's answer or https://www.checkoutall.com/Blog/Index/201410240204236271/ASP-NET-WEB-API-Part-6-controlling-members-serialization – Doug Nov 03 '15 at 17:48
  • 10
    This is much more informative than the accepted answer. – pvgoran Jan 11 '17 at 09:29
  • Is there any way to ignore that member and continue serializing the data structure/class? – ryanwebjackson Nov 01 '17 at 22:03
  • 1
    How about excluding a PROPERTY when [Serializable] was used ? – Daniel Feb 13 '19 at 14:32
  • [Serializable] doesn't serialize the properties, it serializes fields. If you use auto-generated property (e.g. public string Property { get; set; }) then you won't be able to change its serialization. You'll need to add a storage field to the property and add [NonSerialized] in front of that field. – Doug Mar 19 '19 at 17:41
  • 1
    @Daniel: For auto-generated properties, use `[field:NonSerialized]`. – Heinzi Dec 13 '22 at 08:26
  • @Doug: *"If you use auto-generated property (e.g. public string Property { get; set; }) then you won't be able to change its serialization."* That is incorrect, see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.3/auto-prop-field-attrs – Heinzi Dec 13 '22 at 14:14
189

You might be looking for IgnoreDataMemberAttribute.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
  • 5
    My domain layer didn't have the proper assembly reference, then I miss-read the documentation and ended up thinking it was a new feature of .NET 4.5. Tried to find back this thread to delete my comment without success (was in a hurry). Besides, looking at this answer's date should have ringed a bell.... 2009 .NET 4.5 post...) Sorry for wasting your time. – Pluc Mar 07 '13 at 21:16
  • 3
    On .NET 4.5 here: System.Runtime.Serialization.IgnoreDataMemberAttribute works for any thing – SparK Dec 09 '13 at 19:38
  • 1
    Even 3rd-party libraries have added support for [IgnoreDataMember], like JSON.NET. – Doug Nov 03 '15 at 17:49
  • 7
    Wouldn't a short example make this an answer actually worth of the votes? – TaW Dec 29 '18 at 11:11
  • 7
    This answer is incomplete and thus misleading. `[IgnoreDataMember]` would not work if the class has `[Serializable]` attribute. See details in the answer by [Doug](https://stackoverflow.com/a/1792051/264097). – Alex Fainshtein Jun 21 '20 at 19:23
  • In a class annotated with `[DataContract]`, the data contract serializer (as well as other serializers supporting data contract annotations, such as JSON.NET) ignore unannotated properties by default, so `[IgnoreDataMember]` has no effect. – Florian Winter Feb 18 '21 at 13:22
40

In XML Serializing, you can use the [XmlIgnore] attribute (System.Xml.Serialization.XmlIgnoreAttribute) to ignore a property when serializing a class.

This may be of use to you (Or it just may be of use to anyone who found this question when attempting to find out how to ignore a property when Serializing in XML, as I was).

Kris Adams
  • 680
  • 6
  • 11
  • 4
    Ah negative vote. Just thought I would post that because when I searched for how to ignore a property when serializing a class, this post came up. And I wanted to put it here if anyone found this post the same way I did. – Kris Adams Nov 27 '13 at 18:42
  • [in meta] I think they voted negative because you should open another (more especific) question about XMLSerialization and answer it, so people would find that question more relevant in their search – SparK Dec 09 '13 at 19:49
  • 12
    This answer is exactly what I was looking for – aclave1 Oct 13 '14 at 18:01
  • This is precisely what I needed. – DankMemester 'Andrew Servania' Jun 15 '22 at 13:53
3

Try marking the field with [NonSerialized()] attribute. This will tell the serializer to ignore the field.

https://msdn.microsoft.com/en-us/library/system.nonserializedattribute(v=vs.110).aspx

Protector one
  • 6,926
  • 5
  • 62
  • 86
Cris Valenzuela
  • 372
  • 3
  • 9
0

What you are saying is in conflict with what it says in the MSDN library at this location:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

I don't see any mention of the SP1 feature you mention.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 1
    You can find the doc here (3rd paragraph, 2nd sentence): http://msdn.microsoft.com/en-us/library/ms733127.aspx – NotDan Nov 24 '09 at 18:45
  • You're right about the link you posted though, I'm not sure why they don't mention it there, too. – NotDan Nov 24 '09 at 18:49