C Serialization Attributes

Posted on  by 

C Serialization AttributesAttributes

Hi, I have this XML file with element and attributes, the problem is some of the elements has null values after deserializing the xml. And how to access the attributes like for example the name of the Student, number or row/column of Seat?

The concept of Serialization and deserialization is used whenever data pertaining to objects have to be sent from one application to another. Serialization is used to export application data into a file. The destination application then uses deserialization to extract the data from the application for further use.

Serialization is a concept in which C# class objects are written or serialized to files. Let' say you had a C# class called Tutorial. And the class has 2 properties of ID and Tutorials name.

Serializing can be used to directly write the data properties of the Tutorial class to a file. Deserialization is used to read the data from the file and construct the Tutorial object again.

Serialization

Let's look at an example of how we can achieve this.

In our example, we are going to perform the below high-level steps in the code

  1. Create a class called Tutorial which has 2 properties, namely ID, and Name
  2. We will then create an object from the class and assign a value of '1' to the ID property and a value of '.Net' to the name property.
  3. We will then use serialization to serialize the above object to a file called Example.txt
  4. Finally, we will use deserialization to deserialize the object from the file and display the values in the Console.

Enter the below code in the program.cs file of the console application.

Step 1) The first step is to add the class which will be used for serialization

Code Explanation:-

  1. The class which needs to be serialized needs to have the [Serializable] attribute. This is a keyword in C#. This keyword is then attached to the Tutorial class. If you don't mention this attribute, you will get an error when you try to serialize the class.
  2. Next is the definition of the class which will be serialized. Here we are defining a class called 'Tutorial' and providing 2 properties, one is 'ID' and the other is 'Name.'

Step 2) In this step, first we will create the object of the Tutorial class and serialize it to the file called Example.txt

Code Explanation:-

  1. First, we create an object of the Tutorial class. We then assign the value of '1' to ID and '.net' to the name property.
  2. We then use the formatter class which is used to serialize or convert the object to a binary format. The data in the file in serialization is done in binary format. Next, we create a file stream object. The file stream object is used to open the file Example.txt for writing purposes. The keywords FileMode.Create and FileMode.Write is used to specifically mention that the file should be opened for writing purposes.
  3. Finally, we use the Serialize method to transfer the binary data to the file. We then close the stream, since the write operation is complete.

C# Attributes Example

Step 3) Finally to ensure that the data is present in the file, we use deserialization to deserialize the object from the file.

Code Explanation:-

  1. We create the object 'stream' to open the file Example.txt in reading only mode.
  2. We then use the formatter class which is used to deserialize the object, which is stored in the Example.txt file. The object returned is set to the object objnew.
  3. Finally, we display the properties of the object 'objnew' to the console using the 'ID' and 'name' properties.

When the above code is set, and the project is run using Visual Studio, you will get the below output.

Output:-

You can see from the above output that the values from the file were deserialized properly and displayed in the console.

C Sharp Attributes

Summary

C# Attribute List

Serialization is used to write class objects to files.

C Attribute Constructor

De-Serialization is used to recover the objects from the file.

Coments are closed