In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how C # implements serialization and deserialization. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Net there is a technology called object serialization, to put it more colloquially, C # serialization is to save an object to a file or database field, and C # deserialization is to convert the file into the original object when needed.
There are also three common methods of C # serialization in .NET: binary serialization, XML serialization, and SOAP serialization.
Here is a small example to illustrate the use of these three methods.
Using System; using System.Collections.Generic; using System.Text; namespace FileSerializer {[Serializable] public class Book {string id; string name; public string Id {get {return id;} set {id = value;}} public string Name {get {return name } set {name = value;}} public Book () {} public Book (string id,string name) {this.id = id; this.name = name } public override string ToString () {return "No.:" + id + "\ t name:" + name;} using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml.Serialization; namespace FileSerializer {public abstract class Serializer
< T>{string filePath; public string FilePath {get {return filePath;} set {filePath = value;}} public Serializer (string filePath) {this.filePath = filePath } public void Serialize (T serializeObj) {using (Stream st = new FileStream (filePath, FileMode.Create, FileAccess.Write)) {S (st, serializeObj);}} protected abstract void S (Stream st, T serializeObj) Public T Deserialize () {using (Stream st = new FileStream (filePath, FileMode.Open, FileAccess.Read)) {return D (st);}} protected abstract T D (Stream st);}} using System; using System.Collections.Generic; using System.Text; using System.IO Using System.Runtime.Serialization.Formatters.Binary; namespace FileSerializer {class SerializerBinary
< T>: Serializer
< T>{public SerializerBinary (string filePath): base (filePath) {} protected override void S (Stream st, T serializeObj) {BinaryFormatter bf = new BinaryFormatter (); bf.Serialize (st, serializeObj);} protected override T D (Stream st) {BinaryFormatter bf = new BinaryFormatter () Return (T) bf.Deserialize (st);}} using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Soap; namespace FileSerializer {public class SerializerSoap
< T>: Serializer
< T>{public SerializerSoap (string filePath): base (filePath) {} protected override void S (Stream st, T serializeObj) {SoapFormatter sf = new SoapFormatter (); sf.Serialize (st, serializeObj);} protected override T D (Stream st) {SoapFormatter sf = new SoapFormatter () Return (T) sf.Deserialize (st);}} using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization; using System.IO; namespace FileSerializer {public class SerializerXml
< T>: Serializer
< T>{public SerializerXml (string filePath): base (filePath) {} protected override void S (Stream st, T serializeObj) {XmlSerializer xs = new XmlSerializer (typeof (T)); xs.Serialize (st, serializeObj) } protected override T D (Stream st) {XmlSerializer xs = new XmlSerializer (typeof (T)); return (T) xs.Deserialize (st);}} using System; using System.Collections.Generic; using System.Text Namespace FileSerializer {class Program {static void Main (string [] args) {Book book = new Book ("01", "introduction to C# programming 01"); Serializer
< Book>Serializer = new SerializerBinary
< Book>("bookBinary"); serializer.Serialize (book); Book newbook = serializer.Deserialize (); Console.WriteLine (newbook.ToString ()); book = new Book ("02", "introduction to C# programming 02"); serializer = new SerializerSoap
< Book>("bookSoap.soap"); serializer.Serialize (book); newbook = serializer.Deserialize (); Console.WriteLine (newbook.ToString ()); book = new Book ("03", "introduction to C# programming 03"); serializer = new SerializerXml
< Book>("bookXml.xml"); serializer.Serialize (book); newbook = serializer.Deserialize (); Console.WriteLine (newbook.ToString ()); Console.ReadLine ();}} Thank you for reading! This is the end of the article on "how to serialize and deserialize C#". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.