Data Contract Serializer Attributes

Active3 years, 8 months ago
i3arnonData contract serializer attributes pdf
86.5k21 gold badges243 silver badges284 bronze badges
user496949user496949

The Data Contract Serializer used by default in WCF does not support XML attributes for performance reasons (the DCS is about 10% faster on average than the XML serializer). So if you really want to use the DCS, you cannot use this structure you have - it would have to be changed. Or you need to use the XmlSerializer with WCF. Note; Json.NET attributes take precedence over standard.NET serialization attributes (e.g. If both JsonPropertyAttribute and DataMemberAttribute are present on a property and both customize the name, the name from JsonPropertyAttribute will be used). Mar 30, 2017  This article is based on the JSON serialization sample. To define the data contract for a Person type. Define the data contract for Person by attaching the DataContractAttribute to the class and DataMemberAttribute attribute to the members you want to serialize. For more information about data contracts, see Designing service contracts. Apply the DataMemberAttribute attribute in conjunction with the DataContractAttribute to identify members of a type that are part of a data contract. One of the serializers that can serialize data contracts is the DataContractSerializer. The data contract model is an 'opt-in' model. Sep 16, 2010  To support deserializing an object from the attributes, all you have to do is to implement the IXmlSerializable.ReadXml method. Finally, as the aforementioned MSDN article says about the supported types – you should also be able to use XmlElement/XmlNode types as a way of representing XML directly – the DataContractSerializer, like in this case, take the short route and simply gets the Xml.

34k125 gold badges282 silver badges401 bronze badges

6 Answers

See a great comparison of XmlSerializer and DataContractSerializer on Dan Rigsby's blog.

Some points in favor of DataContractSerializer:

  • about 10% faster than XmlSerializer
  • will serialize anything decorated with a [DataMember] - even if it's not public visible
  • will not serialize anything unless you specifically tell it to ('opt-in')
  • you can define the order in which the elements are serialized using the Order= attribute on the [DataMember]
  • doesn't require a parameterless constructor for deserialization
marc_smarc_s
607k138 gold badges1160 silver badges1292 bronze badges

There is one question that hasn't been answered yet by the others here: What if you use [Serializable], but still use DataContractSerializer to serialize that type? Is it any different from serializing a [DataContract] type with DataContractSerializer?

The answer: it makes absolutely no difference! The reason is that when you pass in any supported type to DataContractSerializer, on the first serialization or deserialization episode, it 'shreds' the type to an internal structure that contains all the information about the type's members etc. It then uses the cached internal structure (preserved using dynamic IL) for subsequent serialization episodes, never going back to the original type.

Also, if you're comparing DataContractSerializer's serialization of [Serializable] types vs. XmlSerializer's serialization of those same types, you will find DataContract's serialization to be exponentially faster. A little bit of this is visible in the performance comparison whitepaper available here: http://msdn.microsoft.com/en-us/library/bb310550.aspx

i3arnon
86.5k21 gold badges243 silver badges284 bronze badges
krisragh MSFTkrisragh MSFT
1,8881 gold badge12 silver badges21 bronze badges

You can also keep in mind that the DataContract is more consumer-oriented than XmlSerializer.

While the XmlSerializer has a pure technical dimension ('how do I transform this object into XML'), the DataContract is the public-facing representation of a business concept.

As such the DataContract acts as a reminder that every change you make to your class will have an impact on consumers and that you are implicitly bound by this contract. Conceptually, the DataContract is a central element of your service architecture, XmlSerializer is just a helper.

i3arnon
86.5k21 gold badges243 silver badges284 bronze badges
Johann BlaisJohann Blais
8,3806 gold badges39 silver badges61 bronze badges

There is no real base for comparison if all you do is serialize to and deserialize from XML, apart from syntactical differences and minor features. The most powerful advantage of DataContract over Serializable - something which everyone seems to skip over - is that data contracts are not XML specific.

Working with data contracts, there are only two logical constructs: data contracts and data members (members of a data contract). There are no such things in a data contract as 'elements', or 'xml elements'.

Considering that many computers do not ship with a floppy drive anymore, this is a great tool to add to your portable collection.Virtual Floppy Drive ScreenshotExtracted Size: 416 KBAuthors Website:License: Open Source How to make a Portable Virtual Floppy Drive. This software enables a user to mount floppy images as a virtual floppy drives. Launch a program from the virtual floppy, or view, edit, rename, delete and create files on the virtual floppy. Virtual Floppy Drive (VFD) is a free floppy drive emulator that can be run from a portable device. The user can then directly access the contents to perform the same tasks they would from a physical floppy drive. Virtual floppy drive software.

In theory, data contract based serialization enables an object structure to be represented in any data exchange format, although AFAIK only XML and JSON serialization/deserialization are available in the .NET framework at the moment. However, it is certainly possible to create a serializer than operates with, say, RDF, and I suspect there are additional serializers out there.

Datacontract Serialization

Consider the below simple architecture:

C#

This contract is not XML specific. You didn't specify anything XML-related. You can serialize an instance of the above architecture either into XML:

XML

.. or into JSON:

JSONBootable windows xp iso download.

.. or into any arbitrary data exchange format, as long as you are in possession of a serializer for that format.

This is very powerful and flexible, especially if you have a webservice that is consumed by multiple types of clients and peers, or if the consumer group of your webservice is subject to expansion in the future:

  • JSON is easier to serialize/deserialize in JavaScript and PHP servers (or any dynamic language),
  • XML is more straightforward to serialize/deserialize in WCF clients.

If you don't want to constrain yourself to XML as the only data exchange format in your application architecture, I recommend you go with DataContract. Otherwise, Serializable is slightly more useful for XML only.

Data Contract Serializer AttributesJohn WeiszJohn Weisz
14.5k6 gold badges53 silver badges95 bronze badges

Key issues with XmlSerializer to serialize .NET types to XML

Datacontractserializer Deserialize Xml Attributes

  • Only Public fields or Properties of .NET types can be translated into XML
  • Only the classes which implement IEnumerable interface
  • Classes that implement the IDictionary interface, such as Hash table cannot be serializedImportant difference between DataContractSerializer and XMLSerializer

A practical benefit of the design of the DataContractSerializer is better performance over Xmlserializer.

  • XML Serialization does not indicate which fields or properties of the type are serialized into XML whereas DataCotractSerializer
  • Explicitly shows the which fields or properties are serialized into XML
  • The DataContractSerializer can translate the HashTable into XML
Pranay RanaPranay Rana
146k33 gold badges205 silver badges236 bronze badges

Although this question is quite old but I will summarize my understanding :

1) Datacontract gives you the flexibility of serializing certain members or fields using (DataMember) attribute.2) You can decide with DataContract the order in which your objects will be serialized.

Apart from the obvious things as DataContract serializes your public members whereas Serializable will by default serialize your fields present.

ApoorvaApoorva

Not the answer you're looking for? Browse other questions tagged c#.netwcfserialization or ask your own question.