Serialization and Deserialization in C#

PHPz
Release: 2023-08-28 22:49:08
forward
1348 people have browsed it

C# 中的序列化和反序列化

Serialization converts an object into a stream of bytes and converts it into a form that can be written to the stream. This is done to save it to memory, file or database.

The following serialization operations can be performed:

Binary serialization

All members, even read-only members, will be serialized.

XML Serialization

It serializes the public fields and properties of an object into an XML stream that conforms to a specific XML schema definition language document.

Let's look at an example. First set up the stream:

FileStream fstream = new FileStream("d:\ew.txt", FileMode.OpenOrCreate);
BinaryFormatter formatter=new BinaryFormatter();
Copy after login

Now create an object of this class and call the constructor with three parameters -

Employee emp = new Employee(030, "Tom", “Operations”);
Copy after login

Perform the serialization.

formatter.Serialize(fStream, emp);
Copy after login

Deserialization is the reverse process of serialization, through which you can read objects from a byte stream.

formatter.Deserialize(fStream);
Copy after login

The above is the detailed content of Serialization and Deserialization in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template