Serialized XML (3)

zhaozj2021-02-16  52

From Serialized XML (1), serialization XML (2) can be convenient, simple serialization is XML format, in addition to the objects can be serially connected to the XML format, but also serially For binary, SOAP format.

NET Framework provides automatic Serialization mechanisms via REFLECTION. When an object is serialized, its class name, assembly, and all data members of the class instance will be written into the storage medium. The Serialization Engine maintains tracking of all objects that have been serialized to ensure that the same object reference is only serialized once. Typically, a serialization process will be triggered by the SERIALIZE method of formatter (such as binaryformatter, soapFormatter).

A class can be serialized in two ways:

¨ 简 Simply marked this class as Serializable

¨ Implement the iSerializable interface for this class and mark this Class as Serializable. Declare a class that can be serialized

_ _

Public Class Book

Public bookname as string

Public bookid as integer

END CLASS

Use binaryformatter to sequence the above class into binary format file book.dat, binaryformatter

System.Runtime.Serialization.formatters.binary namespace

DIM Book As New Book

Book.bookid = 1

Book.bookname = "Mathematics"

DIM FORMATTER AS BINARYFORMATTER = New BinaryFormatter

Dim Stream As Stream = New FileStream ("Book.dat", FileMode.create, FileAccess.write, Fileshare.none

Formatter.Serialize (stream, book)

stream.close ()

The data of the binaryformatter serialization is still able to come back from the DESERIALIZE.

DIM FORMATTER AS BINARYFORMATTER = New BinaryFormatter

Dim Stream As Stream = New FileStream ("Book.dat", FileMode.Open, FileAccess.Read, Fileshare.none

Dim book as book = ctype (formatter.deSerialize (stream), book

stream.close ()

Messagebox.show ("Book Name:" & Book.bookName & Vbcrlf & "Book ID:" & book.bookid)

As with the serialization of XML, you can ignore any domain, you can use the NonSerialized property to choose

_ _

Public bookname as string

Similarly to similar methods can also be sequenced into SOAP formats, we use SOAPFormatter.

DIM Book As New Book

Book.bookid = 1

Book.bookname = "中文"

Dim Formatter as soapformatter = New SOAPFORMATTER

Dim Stream As Stream = New FileStream ("Book.xml", FileMode.create, FileAccess.write, Fileshare.none

Formatter.Serialize (stream, book)

stream.close ()

The generated BOOK.XML format is:

English

1

to sum up:

Serialization is a .NET a mechanism for implementing object persistence (PERSISTENT). It is a process that converts data in the object into a single element (usually stream). Its inverse process is Deserialization. The core concept of Serialization is to view all data of an object as a separate unit.  Generally, in both cases, it is very necessary for Serialization:

1) When we want to save the current state of the object to the storage medium so that we can reduce the object in the future

2) When we want to put an object from an application space (

Application Domain

) When passing to another application space.

转载请注明原文地址:https://www.9cbs.com/read-23481.html

New Post(0)