XmlSerialize: Serialization is the process of converting an object into a form that is easy to transmit. For example, an object can be serialized and transmitted between the client and server over the Internet through HTTP. On the other hand, deserialization reconstructs objects in the stream.
XML serialization only serializes public fields and attribute values of objects into XML streams. XML serialization does not include type information. For example, ifLibraryNamespace existsBookObject, it cannot be guaranteed to be deserialized into an object of the same type.
Note::
XML serialization cannot convert methods, indexers, private fields, or read-only attributes (except for read-only sets ). To serialize all public and private fields and attributes of an object, use BinaryFormatter instead of XML serialization.
SoapFormaterr:
Serialize and deserialize the image of an object or the entire connected object in SOAP format.
BinaryFormatter:
Serialize and deserialize an object or the entire connected object in binary format.
You can use BinaryFormatter and SoapFormatter to delegateSerialization, but cannot pass xmlSerializeSerialization.
Using System;
Using System. Runtime. Serialization. Formatters. Soap;
Using System. IO;
Using System. Runtime. Serialization. Formatters. Binary;
Using System. Xml;
Using System. Xml. Serialization;
Class Program
{
Delegate void foo (string formatter );
Static void Main (string [] args)
{
Foo TestHandler = new foo (Test );
BinaryFormatter bFormatter = new BinaryFormatter ();
SoapFormatter sFormatter = new SoapFormatter ();
Try
{
XmlSerializer xFormatter = new XmlSerializer (typeof (foo ));
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
TestHandler ("None ");
String filePath = AppDomain. CurrentDomain. BaseDirectory;
String fileName = "soap. xml ";
String fullPath = Path. Combine (filePath, fileName );
String fileName2 = "binary.txt ";
String fullPath2 = Path. Combine (filePath, fileName2 );
// String fileName3 = "xml. xml ";
// String fullPath3 = Path. Combine (filePath, fileName3 );
Using (FileStream fs = new FileStream (fullPath, FileMode. Create ))
{
SFormatter. Serialize (fs, TestHandler );
Fs. Close ();
}
Using (FileStream fs2 = new FileStream (fullPath2, FileMode. Create ))
{
BFormatter. Serialize (fs2, TestHandler );
Fs2.Close ();
}
Using (FileStream fs = new FileStream (fullPath, FileMode. Open ))
{
Foo deserHandler = (foo) sFormatter. Deserialize (fs );
DeserHandler ("Soap ");
Fs. Close ();
}
Using (FileStream fs = new FileStream (fullPath2, FileMode. Open ))
{
Foo deserHandler = (foo) bFormatter. Deserialize (fs );
DeserHandler ("Binarry ");
Fs. Close ();
}
Console. ReadLine ();
}
Static void Test (string formatter)
{
Console. writeLine ("Formatter: {2} Test invoked. call Time: {0} ticks: {1} ", DateTime. now. toString (), DateTime. now. ticks. toString (), formatter );
}
}
Running result: