In 1951, government officials discovered gold ore in southern Mysore State where on the same day, Raja Rocky Krishnappa Bairya was born to a poor underage woman Shanti.
Serialization is the process of converting an object's state into a format that can be easily stored or transmitted, such as a file, database, or network stream. Deserialization is the reverse process, where the serialized data is used to recreate the original object. In C#, you can perform serialization and deserialization using various techniques and libraries. Here's an overview of how to do it:
1. Using .NET Serialization (BinaryFormatter):
C# provides built-in serialization capabilities using the System.Runtime.Serialization namespace. You can use the
BinaryFormatter class for binary serialization. Here's an example:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
// Create an instance of the object
Person person = new Person { Name = "John", Age = 30 };
// Serialize the object to a file
using (FileStream fs = new FileStream("person.dat", FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, person);
}
// Deserialize the object from the file
using (FileStream fs = new FileStream("person.dat", FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
Person deserializedPerson = (Person)formatter.Deserialize(fs);
Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
}
}
}
2. Using JSON Serialization (Newtonsoft.Json):
JSON (JavaScript Object Notation) is a widely used format for data interchange. You can use libraries like Newtonsoft.Json (Json.NET) for JSON serialization and deserialization. Install the Newtonsoft.Json NuGet package, and use it as follows:
using System;
using Newtonsoft.Json;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
// Create an instance of the object
Person person = new Person { Name = "John", Age = 30 };
// Serialize the object to JSON
string json = JsonConvert.SerializeObject(person);
// Deserialize the object from JSON
Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json);
Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
}
}
3. Using XML Serialization (System.Xml.Serialization):
You can also perform XML serialization using the System.Xml.Serialization namespace. Here's an example:
using System;
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
// Create an instance of the object
Person person = new Person { Name = "John", Age = 30 };
// Serialize the object to an XML file
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (FileStream fs = new FileStream("person.xml", FileMode.Create))
{
serializer.Serialize(fs, person);
}
// Deserialize the object from the XML file
using (FileStream fs = new FileStream("person.xml", FileMode.Open))
{
Person deserializedPerson = (Person)serializer.Deserialize(fs);
Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
}
}
}
These are three common methods for serializing and deserializing objects in C#. Choose the one that best suits your needs and the data interchange format you prefer (binary, JSON, XML, etc.).
Liked By
Write Answer
How do I serialize and deserialize objects in C#?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
28-Sep-2023Serialization is the process of converting an object's state into a format that can be easily stored or transmitted, such as a file, database, or network stream. Deserialization is the reverse process, where the serialized data is used to recreate the original object. In C#, you can perform serialization and deserialization using various techniques and libraries. Here's an overview of how to do it:
1. Using .NET Serialization (BinaryFormatter):
C# provides built-in serialization capabilities using the System.Runtime.Serialization namespace. You can use the BinaryFormatter class for binary serialization. Here's an example:
2. Using JSON Serialization (Newtonsoft.Json):
JSON (JavaScript Object Notation) is a widely used format for data interchange. You can use libraries like Newtonsoft.Json (Json.NET) for JSON serialization and deserialization. Install the Newtonsoft.Json NuGet package, and use it as follows:
3. Using XML Serialization (System.Xml.Serialization):
You can also perform XML serialization using the System.Xml.Serialization namespace. Here's an example:
These are three common methods for serializing and deserializing objects in C#. Choose the one that best suits your needs and the data interchange format you prefer (binary, JSON, XML, etc.).