DataContractSerializer: How to serialize classes/members without DataContract/DataMember attributes

13,987

You cannot - plain and simple. The attribute are needed for the DataContractSerializer to pick up which elements to serialize. In contract to the XmlSerializer, which basically just serializes everything (unless you explicitly tell it to ignore it), the DataContractSerializer is "opt-in" - you have to explicitly tell it (by means of the attributes) which fields and/or properties to serialize.

UPDATE: As several folks have pointed out, with .NET 3.5 SP1, Microsoft loosened those rules up a bit - any public read/write property will be serialized automatically by the DataContractSerializer. At the same time, your class also needs to have a parameterless default constructor - sounds like the exact requirements we had for XmlSerializer way back when....

Of course, this:

  • doesn't allow you to serialize something private - if you want to serialize it, you have to expose it as public read/write property
  • doesn't allow you to specify a defined chosen ordering of the parameters - it will just use them in the order they appear in the class
  • now requires you to have a parameterless constructor in your class again for deserialization

I still think these thing ought to be explicit and clear, making those no longer required opens up the path for lazy/sloppy programming - I don't like it. But if you want to, you can use it now without explicitly marking your properties with [DataMember].....

Marc

Share:
13,987
aleemb
Author by

aleemb

I am a web development junkie and love the whole stack. SOreadytohelp

Updated on July 20, 2022

Comments

  • aleemb
    aleemb almost 2 years

    DataContractSerializer requires classes and members to be marked with the DataContract and DataMember attributes. However, in my case the classes are auto-generated with the EFPocoAdapater framework and these attributes are not present.

    How can I force serialization of all members using the DataContractSerializer without these attributes being present?

    From Alexdej:

    This changed in 3.5SP1, hope you saw that: http://www.pluralsight.com/community/blogs/aaron/archive/2008/05/13/50934.aspx

  • aleemb
    aleemb about 15 years
    I was really hoping you'd be wrong but after much googling I'm afraid you're right. Can't use XmlSerializer (circular reference problem working with the Entity Framework). Ah well, anonymous classes it is.
  • marc_s
    marc_s about 15 years
    Well, I was reading up a bit more on this today, and here I found that if your classes used by the DataContractSerializer are marked with [Serializable], then by default they will be serialized much like the old-style SOAP formatter - EVERY single field regardless of visibility is included. Could that be of help to you maybe?
  • aleemb
    aleemb almost 15 years
    Well not really because my problem really is that I can't touch the auto-generated classes and I need to be able to externally ignore fields. I am using projections in my linq queries to get around this now which works fine.
  • NotDan
    NotDan over 14 years
    This answer is no longer correct (see alexdej's answer). Since it is accepted, it should probably be edited to reflect the changes in 3.5SP1.
  • marc_s
    marc_s over 14 years
    @Notdan: I still think it's better practice to have these things EXPLICITLY and CLEARLY in your code. But you're right - with 3.5 SP1 it's no longer required..... (welcome sloppy programming!)