WCF serialization and wcf deserialization

Source: Internet
Author: User
Tags xml attribute

WCF serialization and wcf deserialization

In WCF, a class is provided specifically for serialization and deserialization operations. This class is the DataContractSerializer class. Generally, WCF automatically uses DataContractSerializer to serialize the serializable session data contract, which is not directly called by developers. In addition to the DataContractSerializer class for serialization, WCF also supports two serializers: XMLSerializer (defined in System. XML. serialization namespace) and NetDataContractSerializer (defined in System. XML. serialization namespace ). The XmlSerializer class is not a specialized WCF class. The Asp.net Web Service uses this class as the serializer. However, the XmlSerializer class supports less classes than the types supported by the DataContractSerializer column, however, it allows more control over the generated XML and supports more XML Schema Definition Language (XSD) standards. It does not need to have any declarative attributes on the serializable class.

The DataContractSerializer class is similar to the NetDataContractSerializer class. The main difference between them is that the serialization type does not need to be specified when NetDataContractSerializer is used for serialization. For example:

ANetDataContractSerializer serializer = new NetDataContractSerializer (); // you do not need to specify the serializer type explicitly. writeObject (writer, p); // while using DataContractSerializer, you must specify the serialization type DataContractSerializer serializer = new DataContractSerializer (typeof (Order); // you must specify the serialization type serializer explicitly. writeObject (writer, p );

The ing between the XML file generated using the DataContractSerializer class and the original object is as follows (default ):

1. The Root Element is the Type Name of the object -- DataContractOrder

2. The Namespace of Type is added to the Namespace of the XML root node.

3. All the members of the object are output in the form of XML Element rather than XML Attribute.

4. Therefore, the output order of objects in XML is in alphabetical order.

5. Elelement names of all Members are member names.

6. No matter what scope (public, protected, internal, or even municipal Private) The member sets ),

All members using DataMemberAttribute are serialized into XML,

7. Type and members must use DataContractAttribute and DataMemberAttribute to be serialized.

You can use these two attributes (DataContractAttribute and DataMemberAttribute) to create relevant parameters to generate the required XML structure.

1. The Root Element can be defined by the Name parameter in DataContractAttribute.

2. The Namespace can be defined through the NameSpace parameter in DataContractAttribute.

3. Object members can only be serialized as XML elements.

4. the XML Element corresponding to the object member can be defined by the Order parameter of DataMemberAttribute in the XML format.

5. The Name of the Element corresponding to the object member can be defined by the Name in DataMemberAttribute.

6. If you do not want a member to be output to XML, you can remove the DataMemberAttribute Attribute corresponding to the member.

In addition, DataMemberAttribute has an additional parameter:

1. IsRequired: This member is required. If the XSD is generated using a tool, the minOccur of the corresponding Element is "1"

2. emitdefavalue value: determines whether to enter a member (whose value is the default value) that is not assigned a value in XML.

Note: Because DataContractSerializer is used in WCF for serialization and deserialization, the object type must be determined in advance during DataContractSerializer serialization and deserialization. If the serialized object or the object generated by deserialization contains unknown types, serialization or deserialization will fail. Therefore, to ensure the normal serialization and deserialization of DataContractSerializer, you need to add the "unknown" type to the DataContractSerializer "known" type list. You can set KnownTypeAttribute and ServiceKnownTypeAttribute through two features. KnownTypeAttribute is used in a data contract to set sub-data contracts that inherit the data contract type, or to reference other contract types. ServiceKnownTypeAttribute can be applied to service contract interfaces and methods, service implementation classes and methods, and different target elements, which determine the scope of defining known types.

 

The ing between the XML file generated using the XmlSerializer class and the original object is as follows (default ):

1. The Root Element is specified as the class name.
2. The corresponding Namaspace will not be added to the Root Element.
3. The object members are output as XML elements.
4. The object members appear smoothly and in the sequence defined in Type.
5. Only Public Field and readable writable Proppery will be serialized into XML. For example, the internal string ProducingArea defined in XMLProduct does not appear in XML.
6. You do not need to use any Attribute when defining Type.

You can use XML-related attributes to generate the required XML structure.

1. The Root Element name can be the class name.
2. XMLRoot can be used on the Type, and Namespace can be specified on the Root Element through the Namaspace parameter.
3. You can use XMLElement Attribute and XMLAttribute Attribute on Class Members to specify whether the object member is converted to XMLElement or XMLAttribute. The NameSpace parameter can be used to define 4. Namespace.
5. You can use the Order parameter in XMLElement or XMLAttribute to specify the position where a member appears in XML.
6. You can use XmlIgnore attribute to prevent object members from being serialized.

Two different Serializer differences:

Features

XMLSerializer

DataContractSerializer

Default Mapping

All Public fields and readable and writable Properties

All DataMember Filed and Property

Whether Attribute is required

No

DataContract DataMember or Serializable

Default order of members

Sequence defined in Type

Letter Sorting

Compatibility

. Asmx

Remoting

Deserialation

Call the default constructor

Not called

 

 

This article references and references the following articles:

My journey to WCF (4): serialization in WCF [Part 1]

My journey to WCF (4): serialization in WCF [Part II]

Learn about WCF (7) with me -- detailed explanation of WCF data contract and serialization

 

Article published on my personal website: http://www.zuowenjun.cn/post/2015/03/25/134.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.