Home >Backend Development >C#.Net Tutorial >Serialization and Deserialization in C#

Serialization and Deserialization in C#

PHPz
PHPzforward
2023-08-28 22:49:081383browse

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();

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

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

Perform the serialization.

formatter.Serialize(fStream, emp);

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

formatter.Deserialize(fStream);

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete