Datacontractserializer Attributes

Posted on by admin
Datacontractserializer Attributes 5,0/5 8589 votes

It turns out that you can mock up the business objects with attributes for both DataContractSerializer and XmlSerializerFormat, but the interfaces that expose these objects must be different. So you can’t have, say, an IDataService that functions for both a DataContract interface and an Xml interface.

  1. Datacontractserializer Serialize Attributes
-->

Definition

Serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract. This class cannot be inherited.

Inheritance
XmlObjectSerializerXmlObjectSerializerXmlObjectSerializerXmlObjectSerializer
DataContractSerializerDataContractSerializerDataContractSerializerDataContractSerializer

Examples

The following example code shows a type named Person that is serialized by the DataContractSerializer. The DataContractAttribute attribute is applied to the class, and the DataMemberAttribute is applied to members to instruct the DataContractSerializer what to serialize.

Remarks

Use the DataContractSerializer class to serialize and deserialize instances of a type into an XML stream or document. For example, you can create a type named Person with properties that contain essential data, such as a name and address. You can then create and manipulate an instance of the Person class and write all of its property values in an XML document for later retrieval, or in an XML stream for immediate transport. Most important, the DataContractSerializer is used to serialize and deserialize data sent in Windows Communication Foundation (WCF) messages. Apply the DataContractAttribute attribute to classes, and the DataMemberAttribute attribute to class members to specify properties and fields that are serialized.

For a list of types that can be serialized, see Types Supported by the Data Contract Serializer.

To use the DataContractSerializer, first create an instance of a class and an object appropriate to writing or reading the format; for example, an instance of the XmlDictionaryWriter. Then call the WriteObject method to persist the data. To retrieve data, create an object appropriate to reading the data format (such as an XmlDictionaryReader for an XML document) and call the ReadObject method.

For more information about using the DataContractSerializer, see Serialization and Deserialization.

You can set the type of a data contract serializer using the <dataContractSerializer> element in a client application configuration file.

Preparing Classes for Serialization or Deserialization

The DataContractSerializer is used in combination with the DataContractAttribute and DataMemberAttribute classes. To prepare a class for serialization, apply the DataContractAttribute to the class. For each member of the class that returns data that you want to serialize, apply the DataMemberAttribute. You can serialize fields and properties, regardless of accessibility: private, protected, internal, protected internal, or public.

For example, your schema specifies a Customer with an ID property, but you already have an existing application that uses a type named Person with a Name property. To create a type that conforms to the contract, first apply the DataContractAttribute to the class. Then apply the DataMemberAttribute to every field or property that you want to serialize.

Note

You can apply the DataMemberAttribute to both private and public members.

The final format of the XML need not be text. Instead, the DataContractSerializer writes the data as an XML infoset, which allows you to write the data to any format recognized by the XmlReader and XmlWriter. It is recommended that you use the XmlDictionaryReader and XmlDictionaryWriter classes to read and write, because both are optimized to work with the DataContractSerializer.

If you are creating a class that has fields or properties that must be populated before the serialization or deserialization occurs, use callback attributes, as described in Version-Tolerant Serialization Callbacks.

Adding to the Collection of Known Types

When serializing or deserializing an object, it is required that the type is 'known' to the DataContractSerializer. Begin by creating an instance of a class that implements IEnumerable<T> (such as List<T>) and adding the known types to the collection. Then create an instance of the DataContractSerializer using one of the overloads that takes the IEnumerable<T> (for example, DataContractSerializer(Type, IEnumerable<Type>).

Note

Unlike other primitive types, the DateTimeOffset structure is not a known type by default, so it must be manually added to the list of known types (see Data Contract Known Types).

Forward Compatibility

The DataContractSerializer understands data contracts that have been designed to be compatible with future versions of the contract. Such types implement the IExtensibleDataObject interface. The interface features the ExtensionData property that returns an ExtensionDataObject object. For more information, see Forward-Compatible Data Contracts.

Running under Partial Trust

When instantiating the target object during deserialization, the DataContractSerializer does not call the constructor of the target object. If you author a [DataContract] type that is accessible from partial trust (that is, it is public and in an assembly that has the AllowPartiallyTrustedCallers attribute applied) and that performs some security-related actions, you must be aware that the constructor is not called. In particular, the following techniques do not work:

  • If you try to restrict partial trust access by making the constructor internal or private, or by adding a LinkDemand to the constructor -- neither of these have any effect during deserialization under partial trust.

  • If you code the class that assumes the constructor has run, the class may get into an invalid internal state that is exploitable.

Constructors

DataContractSerializer(Type)DataContractSerializer(Type)DataContractSerializer(Type)DataContractSerializer(Type)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type.

DataContractSerializer(Type, DataContractSerializerSettings)DataContractSerializer(Type, DataContractSerializerSettings)DataContractSerializer(Type, DataContractSerializerSettings)DataContractSerializer(Type, DataContractSerializerSettings)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type and settings.

DataContractSerializer(Type, IEnumerable<Type>)DataContractSerializer(Type, IEnumerable<Type>)DataContractSerializer(Type, IEnumerable<Type>)DataContractSerializer(Type, IEnumerable<Type>)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type, and a collection of known types that may be present in the object graph.

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, and a surrogate for custom serialization.

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, and an alternative for mapping xsi:type declarations at run time.

DataContractSerializer(Type, String, String)DataContractSerializer(Type, String, String)DataContractSerializer(Type, String, String)DataContractSerializer(Type, String, String)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type using the supplied XML root element and namespace.

DataContractSerializer(Type, String, String, IEnumerable<Type>)DataContractSerializer(Type, String, String, IEnumerable<Type>)DataContractSerializer(Type, String, String, IEnumerable<Type>)DataContractSerializer(Type, String, String, IEnumerable<Type>)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two string parameters as well as a list of known types that may be present in the object graph.

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, and the XML element and namespace that contain the content.

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, the XML element and namespace that contains the content, and an alternative for mapping xsi:type declarations at run time.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type using the XML root element and namespace specified through the parameters of type XmlDictionaryString.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two XmlDictionaryString parameters as well as a list of known types that may be present in the object graph.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, and parameters of XmlDictionaryString that specify the XML element and namespace that contain the content.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, parameters of XmlDictionaryString that specify the XML element and namespace that contains the content, and an alternative for mapping xsi:type declarations at run time.

Properties

DataContractResolverDataContractResolverDataContractResolverDataContractResolver

Gets the component used to dynamically map xsi:type declarations to known contract types.

DataContractSurrogateDataContractSurrogateDataContractSurrogateDataContractSurrogate

Gets a surrogate type that can extend the serialization or deserialization process.

IgnoreExtensionDataObjectIgnoreExtensionDataObjectIgnoreExtensionDataObjectIgnoreExtensionDataObject

Gets a value that specifies whether to ignore data supplied by an extension of the class when the class is being serialized or deserialized.

KnownTypesKnownTypesKnownTypesKnownTypes

Gets a collection of types that may be present in the object graph serialized using this instance of the DataContractSerializer.

MaxItemsInObjectGraphMaxItemsInObjectGraphMaxItemsInObjectGraphMaxItemsInObjectGraph

Gets the maximum number of items in an object graph to serialize or deserialize.

PreserveObjectReferencesPreserveObjectReferencesPreserveObjectReferencesPreserveObjectReferences

Gets a value that specifies whether to use non-standard XML constructs to preserve object reference data.

SerializeReadOnlyTypesSerializeReadOnlyTypesSerializeReadOnlyTypesSerializeReadOnlyTypes

Gets a value that specifies whether read-only types are serialized.

Methods

Equals(Object)Equals(Object)Equals(Object)Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()GetHashCode()GetHashCode()GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()GetType()GetType()GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsStartObject(XmlDictionaryReader)IsStartObject(XmlDictionaryReader)IsStartObject(XmlDictionaryReader)IsStartObject(XmlDictionaryReader)

Determines whether the XmlDictionaryReader is positioned on an object that can be deserialized.

IsStartObject(XmlReader)IsStartObject(XmlReader)IsStartObject(XmlReader)IsStartObject(XmlReader)

Determines whether the XmlReader is positioned on an object that can be deserialized.

MemberwiseClone()MemberwiseClone()MemberwiseClone()MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ReadObject(Stream)ReadObject(Stream)ReadObject(Stream)ReadObject(Stream)

Reads the XML stream or document with a Stream and returns the deserialized object.

(Inherited from XmlObjectSerializer)
ReadObject(XmlDictionaryReader)ReadObject(XmlDictionaryReader)ReadObject(XmlDictionaryReader)ReadObject(XmlDictionaryReader)

Reads the XML document or stream with an XmlDictionaryReader and returns the deserialized object.

(Inherited from XmlObjectSerializer)
ReadObject(XmlDictionaryReader, Boolean)ReadObject(XmlDictionaryReader, Boolean)ReadObject(XmlDictionaryReader, Boolean)ReadObject(XmlDictionaryReader, Boolean)

Reads the XML stream with an XmlDictionaryReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.

ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)

Reads an XML document or document stream and returns the deserialized object. The method includes a parameter to specify whether the object name is verified is validated, and a resolver for mapping xsi:type declarations at runtime.

ReadObject(XmlReader)ReadObject(XmlReader)ReadObject(XmlReader)ReadObject(XmlReader)

Reads the XML stream with an XmlReader and returns the deserialized object.

ReadObject(XmlReader, Boolean)ReadObject(XmlReader, Boolean)ReadObject(XmlReader, Boolean)ReadObject(XmlReader, Boolean)

Reads the XML stream with an XmlReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.

ToString()ToString()ToString()ToString()

Returns a string that represents the current object. Esti ceea ce gandesti wayne dyer pdf books in order.

(Inherited from Object)
WriteEndObject(XmlDictionaryWriter)WriteEndObject(XmlDictionaryWriter)WriteEndObject(XmlDictionaryWriter)WriteEndObject(XmlDictionaryWriter)

Writes the closing XML element using an XmlDictionaryWriter.

WriteEndObject(XmlWriter)WriteEndObject(XmlWriter)WriteEndObject(XmlWriter)WriteEndObject(XmlWriter)

Writes the closing XML element using an XmlWriter.

WriteObject(Stream, Object)WriteObject(Stream, Object)WriteObject(Stream, Object)WriteObject(Stream, Object)

Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified Stream.

(Inherited from XmlObjectSerializer)
WriteObject(XmlDictionaryWriter, Object)WriteObject(XmlDictionaryWriter, Object)WriteObject(XmlDictionaryWriter, Object)WriteObject(XmlDictionaryWriter, Object)

Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified XmlDictionaryWriter.

(Inherited from XmlObjectSerializer)
WriteObject(XmlDictionaryWriter, Object, DataContractResolver)WriteObject(XmlDictionaryWriter, Object, DataContractResolver)WriteObject(XmlDictionaryWriter, Object, DataContractResolver)WriteObject(XmlDictionaryWriter, Object, DataContractResolver)

Writes all the object data (starting XML element, content, and enclosing element) to an XML document or stream using the specified XmlDictionaryWriter. The method includes a resolver for mapping xsi:type declarations at runtime.

WriteObject(XmlWriter, Object)WriteObject(XmlWriter, Object)WriteObject(XmlWriter, Object)WriteObject(XmlWriter, Object)

Writes all the object data (starting XML element, content, and closing element) to an XML document or stream with an XmlWriter.

WriteObjectContent(XmlDictionaryWriter, Object)WriteObjectContent(XmlDictionaryWriter, Object)WriteObjectContent(XmlDictionaryWriter, Object)WriteObjectContent(XmlDictionaryWriter, Object)

Writes the XML content using an XmlDictionaryWriter.

WriteObjectContent(XmlWriter, Object)WriteObjectContent(XmlWriter, Object)WriteObjectContent(XmlWriter, Object)WriteObjectContent(XmlWriter, Object)

Writes the XML content using an XmlWriter.

WriteStartObject(XmlDictionaryWriter, Object)WriteStartObject(XmlDictionaryWriter, Object)WriteStartObject(XmlDictionaryWriter, Object)WriteStartObject(XmlDictionaryWriter, Object)

Writes the opening XML element using an XmlDictionaryWriter.

WriteStartObject(XmlWriter, Object)WriteStartObject(XmlWriter, Object)WriteStartObject(XmlWriter, Object)WriteStartObject(XmlWriter, Object)

Writes the opening XML element using an XmlWriter.

Extension Methods

GetSerializationSurrogateProvider(DataContractSerializer)GetSerializationSurrogateProvider(DataContractSerializer)GetSerializationSurrogateProvider(DataContractSerializer)GetSerializationSurrogateProvider(DataContractSerializer)

Returns the surrogate serialization provider for this serializer.

SetSerializationSurrogateProvider(DataContractSerializer, ISerializationSurrogateProvider)SetSerializationSurrogateProvider(DataContractSerializer, ISerializationSurrogateProvider)SetSerializationSurrogateProvider(DataContractSerializer, ISerializationSurrogateProvider)SetSerializationSurrogateProvider(DataContractSerializer, ISerializationSurrogateProvider)

Specifies a surrogate serialization provider for this DataContractSerializer.

Applies to

Datacontractserializer attributes of life

Thread Safety

Instances of this class are thread safe except when the instance is used with an implementation of the IDataContractSurrogate or DataContractResolver.

See also

Active2 years, 6 months ago

Is it possible to deserialize this XML into an object marked with the DataContract attribute?

As you may see there is 'units' attribute. I don't believe that's supported. Or am I wrong?

Schultz9999Schultz9999
4,3436 gold badges36 silver badges77 bronze badges

2 Answers

This can be achieved, but you will have to override the default serializer by applying the [XmlSerializerFormat] attribute to the DataContract. Although it can be done, this does not perform as well as the default serializer, so use it with caution.

The following class structure will give you the result you are after:

Datacontractserializer Serialize Attributes

You can test this with the following code:

The output will be:

Greg SansomGreg Sansom
16.1k6 gold badges49 silver badges66 bronze badges

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, as Greg showed in his answer - that works, too, but you lose the performance benefit (plus all other benefits) of the DCS.

CJBS
10.4k4 gold badges58 silver badges103 bronze badges
marc_smarc_s
604k137 gold badges1156 silver badges1291 bronze badges

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