Detailed serialization and deserialization of notes

Source: Internet
Author: User

Serialization Concepts:

Marshal by Value: Transfers an object from one application domain to another, and then deserializes it so that an exact copy of the object is generated in the second application domain.

If the object derives from MarshalByRefObject, the object reference is passed from one application domain to another.

Security issues with custom serialization:

Serialization allows other code to view or modify object data that cannot be accessed in other ways. Therefore, the code that executes the serialization needs to develop the SecurityPermission of the serializationformatter identity. By default, the code downloaded by the network does not grant this permission, only code on the local computer is granted that permission, and the GetObjectData method must be protected in the following ways : Requires the use of the specified The SecurityPermission of the SerializationFormatter flag ,or requires additional permissions that are specifically designed to help protect private data.

Version-tolerant serialization of VTS:

Both binary serialization and Soap serialization Enable the VTS, which,when deserialized, ignores exceptions caused by all extraneous or unexpected data. the optionalfieldattribute attribute modifies the field to mark the field as optional, which is indeed ignored during deserialization when the field cannot be deserialized because of missing data.

The attribute of the Optionalfieldattribute attribute versionadded is used to mark the version of the class , and should be incremented by exactly 1each time the type is modified. Since this property is not used in . Net Framework2.0 , the version number should start at 2 .

UseSerializationbindercontrol of serialization and deserialization:

During serialization, the formatter transmits the information needed to create an object instance of the correct type and version, including the full name of the object and the assembly name. By default, deserialization can use this information to create the same object instance.

However, because the original class may not exist on the computer that is performing the deserialization, the original class has moved between assemblies or the server and the client require a different version, so the user may need to control the specific classes of serialization and deserialization. Child functions can only be used when using BinaryFormatter and netdatacontractserializer .

This feature requires inheriting the abstract class Serializationbinder and overriding the methods bindtoname and bindtotype(which must be overridden, The parameter passed in by this method is the fully qualified name of the assembly name and type of the class being serialized.

Serialization Best Practices:

Never remove a serialized field.

If the old version does not use the noserializedattribute decorated field, the new version should not be used.

Never modify the name or type of a serialized field.

If you add a new field, you should use the optionalfieldattribute attribute decoration.

Fields that remove nonserializedattribute adornments should be decorated with optionalfieldattribute .

For a field that cannot accept 0 or null as the default value, the serialization callback should be used to set a meaningful default value.

If you want to ensure that the type is compatible with the future serialization engine, you should always use the optionalfieldattribute versionadded property to set the version. and avoid version branching.

The serialization of C # is divided into binary serialization,Soap serialization, andXML serialization.

Tool class:

Binary serialization serializes an object into a binary format, using the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter class.

Soap serialization serializes an object into a data format that is transmitted in accordance with the Soap protocol, using the The System.Runtime.Serialization.Formatters.Soap.SoapFormatter class.

XML serialization serializes an object into XML data, using the System.Xml.Serialization.XmlSerializer class implementation.

The different serialization methods:

binary serialization and Soap serialization serialize the serializable public or private fields of an object, andXML Serialization simply serializes a public field or property.

Binary serialization serializes the fully qualified name of the class with the full name of the Assembly (project name, version, public key token, culture, and so on).

Soap serialization uses an XML namespace to serialize the original assembly information.

XML serialization does not save the qualified name and the assembly name, but The deserialization of the XML still cannot be serialized to the other class of the same member. XML Serialization requires the development of the type of the serialized object.

SerializableAttributedescription of the characteristics:

Classes that need to be persisted must be decorated with the SerializableAttribute feature and optionally use nonserializedattribute attribute adornments do not require serialization of properties or fields.

However, the SerializableAttribute attribute adornment is not required when using XML serialization , but requires a parameterless construction method.

binary serialization andSoapSerialization:

See also simple serialization and deserialization of notes.

Note:Soap Serialization does not support generics.

XmlSerialization:

XML serialization does not require the SerializableAttribute attribute to decorate the serialized class, but requires that the parameterless constructor be required.

XML can only serialize exposed properties or fields, and if the property or field being serialized is not displayed using an attribute adornment, a normal property or field, except for the collection property or field, is decorated by default with the xmlelementattribute attribute.

The property or object in the object that is decorated with XmlTextAttribute is serialized as the parent element's text child node instead of the element child node.

XmlElementAttribute can be used to modify the name of an element after a modified property or field is serialized, and the attribute Order can be used to specify that the decorated property or field is serialized into XML The order in which the elements appear.

Class uses the XmlTypeAttribute modifier to modify the name of the corresponding element after serialization for the class.

Class properties Use the XmlAttributeAttribute attribute to serialize a property or field to the XML attribute of the parent element .

The XmlArrayAttribute attribute modifies the name of the element corresponding to the decorated collection member, and if the collection property is decorated with the xmlelementattribute attribute, it is equivalent to using the attribute to decorate each element of the collection property, for example:

public class Womanwithfriend

{

[XmlArray (" friends ")]

[XmlArrayItem (" friends ")]

[XmlElement]

Public list<woman> Friends {get; set;}

Public Woman self {get; set;}

}

public class Woman

{

public string Womanname {get; set;}

public int Age {get; set;}

public string Description {get; set;}

}

Results using the XmlElement adornment:

<?xml version= "1.0"?>

<womanwithfriend xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" >

<friends womanname= " woman 1" age= "> " here is a description of the female human being </Friends>

<friends womanname= " woman 2" age= "> " here is a description of the female human being </Friends>

<friends womanname= " woman 3" age= "> " here is a description of the female human being </Friends>

<self womanname= " woman herself " age= "> " here is the woman herself </Self>

</WomanWithFriend>

The result of using xmlarray and xmlarrayitem adornments without using XmlElement :

<?xml version= "1.0"?>

<womanwithfriend xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" >

<Friends>

<woman womanname= " woman 1" age= "> " here is a description of the female human being </Woman>

<woman womanname= " woman 2" age= "> " here is a description of the female human being </Woman>

<woman womanname= " woman 3" age= "> " here is a description of the female human being </Woman>

</Friends>

<self womanname= " woman herself " age= "> " here is the woman herself </Self>

</WomanWithFriend>

The XmlArrayItemAttribute attribute modifies the name of the XML element corresponding to the element of the decorated collection member, as set by the XmlTypeAttribute of the class of the collection property element . the xml element name is overwritten, and the constructor of the attribute can pass the type value to use polymorphism for the properties of the collection type (the collection property is specified as the base class, But the element of the collection is at least one derived class) which specifies the type of the element of the collection type's property (each xmlarrayitemattribute Specifies a derived class, and if the element of the collection property has more than one derived class, you should use multiple xmlarrayitemattribute characteristics).

Properties or fields that are modified by XmlIgnoreAttribute are not serialized.

the XmlRootAttribute modifier class can be used to set the properties of the serialized root node.

XML Serialization is customized using the ixmlserializable interface, via XmlReader and XmlWriter For the process of serialization and deserialization, code examples:

public class Customerxmlserializable:ixmlserializable

{

public string Name;

Public System.Xml.Schema.XmlSchema GetSchema ()// not known

{

return null;

}

public void ReadXml (System.Xml.XmlReader reader)

{

Name = reader. GetAttribute (" name ");

}

public void WriteXml (System.Xml.XmlWriter writer)

{

Writer. WriteAttributeString (" name ", names);

}

}

Date Modified 2015-09-20

Detailed serialization and deserialization of notes

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.