This article describes the methods of C# serialization and deserialization. Share it for your reference. The specific analysis is as follows:
The process of converting "object" to "byte sequence" is called object serialization. The process of restoring a "byte sequence" to a "object" is called deserialization of an object.
Serialization
using ;
using ;
using ;
using ;
using ;
namespace serialization and deserialization
{
//Serialization is to convert objects into binary
//Deserialization is to convert binary into an object
//The function of serialization and deserialization is to transmit data.
//When we transmit data on the network, only binary can be transmitted. In other words, no matter what content we transmit on the network, we will first serialize the content you want to transmit into binary data. The other party receives a binary data, which needs to deserialize the binary data into an object.
//If we want to serialize a class, the first step is to mark the class as serializable. Use the [Serializable] keyword. Only objects created by classes marked with [Serializable] can be serialized
[Serializable] //The function of this [Serializable] is to indicate that a class can be serialized. This kind cannot be inherited.
public class Person
{
public string Name { get; set; }
public int Age{get;set;}
public char Gender{get;set;}
public Person() { }
public Person(string name, int age, char gender)
{
= name;
= age;
= gender;
}
}
class Program
{
static void Main(string[] args)
{
//Now we want to transmit the p object to the other party’s computer. Now that there is no other party's computer, I will simulate it on my computer: now convert the p object into binary through a stream and write it to my desktop. Then we deserialize this binary file into an object
Person p = new Person("Xuehui", 25, 'female');
using (FileStream stream = new FileStream(@"C:\Users\Fanbin\Desktop\", , ))
{
//Start serializing the object. You need a class to start serializing an object
BinaryFormatter bf = new BinaryFormatter();
//public void Serialize(Stream serializationStream, object graph); The first parameter of this Serialize method is: the stream file to be serialized into, and the second parameter is: the object to be serialized
(stream, p);
}
("Serialization is completed");
}
}
}
Deserialization
using ;
using ;
using ;
using ;
using ;
namespace serialization and deserialization
{
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public char Gender { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Now we deserialize the binary stream file that has just been written to the desktop into an object
using (FileStream stream2 = new FileStream(@"C:\Users\Fanbin\Desktop\", , ))
{
BinaryFormatter bf2 = new BinaryFormatter();
//public object Deserialize(Stream serializationStream);The parameters of this Deserialize method are: the binary stream file to be deserialized. Its return value is an object. All we want to force this object to Person type.
Person p2=(Person) (stream2);
("Fang serialization successfully");
(); //Output: Xuehui
();
}
}
}
}
Serialization
Serialize an instance of this class into a file:
[ This file is a file that is stored persistently after serializing the obj object]
obj.n1 = 1;
obj.n2 = 24;
= "Some strings";
IFormatter formatter = new BinaryFormatter();
Stream MyStream = new FileStream("", ,
, );
(MyStream, obj);
();
Deserialization
What we are talking about is to convert a file into an obj object by deserializing it.
[filestream reads this file stream and deserializes it into an object using the .net serializer]
Stream MyStream = new FileStream("", ,
, );
MyObject obj = (MyObject) (MyStream);
();
xml
Serialize an instance of this class into an Xml file
(new FileStream(@"", ), obj);
Deserialization
MyObject my=(MyObject)(new FileStream(@"",));
Serialize DataTable
It is mainly to enable the next time the project is started to read the object information saved last time. To be simple, it is to save an object and restore it when it is special. There are three common serialization methods in C#: BinaryFormatter, SoapFormatter, and XML serialization. What we are involved in today is mainly XML serialization, which is to serialize and save the content of DataTable.
/// DataTable serialization
/// </summary>
/// <param name="dt">DataTable that needs to be serialized</param>
/// <param name="path">path</param>
public void Serializer(DataTable dt,string path)
{
XmlSerializer serializer = new XmlSerializer(typeof());
writer= (path);
(writer, dt);
}
//Deserialization
private void InitData(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof());
FileStream fs = new FileStream(filePath, , , );
DataTable dt = (DataTable)(fs);
for (int i = 0; i < ; i++)
{
int j = ();
[j].SetValues([i]["Device ID"].ToString(),
[i]["Serial Number"].ToString(),
[i]["Device Name"].ToString(),
[i]["number"].ToString(),
[i]["Connection Information"].ToString(),
[i]["Remarks"].ToString(),
[i]["TagID"].ToString());
}
}
I hope this article will be helpful to everyone's C# programming.