網站首頁 編程語言 正文
一.序列化與反序列化解釋
? ? ? ?序列化 (Serialization)是將對象的狀態(tài)信息轉換為可以存儲或傳輸的形式的過程。在序列化期間,對象將其當前狀態(tài)寫入到臨時或持久性存儲區(qū)。以后,可以通過從存儲區(qū)中讀取或反序列化對象的狀態(tài),重新創(chuàng)建該對象。
二.序列化目的
? ?1、以某種存儲形式使自定義對象持久化;
? ?2、將對象從一個地方傳遞到另一個地方;
? ?3、使程序更具維護性等
三.C#中的三種序列化說明
1、以二進制格式序列化
? ? ? ?二進制序列化保持類型保真度,這對于在應用程序的不同調用之間保留對象的狀態(tài)很有用。例如,通過將對象序列化到剪貼板,可在不同的應用程序之間共享對象。您可以將對象序列化到流、磁盤、內存和網絡等等。遠程處理使用序列化“通過值”在計算機或應用程序域之間傳遞對象。
? ? ?引用命名空間:using System.Runtime.Serialization.Formatters.Binary;
2、以SOAP格式序列化
? ? ? SOAP(Simple Object Access Protocol )簡單對象訪問協(xié)議是在分散或分布式的環(huán)境中交換信息的簡單的協(xié)議,是一個基于XML的協(xié)議,它包括四個部分:SOAP封裝(envelop),封裝定義了一個描述消息中的內容是什么,是誰發(fā)送的,誰應當接受并處理它以及如何處理它們的框架;SOAP編碼規(guī)則(encoding rules),用于表示應用程序需要使用的數據類型的實例; SOAP RPC表示(RPC representation),表示遠程過程調用和應答的協(xié)定;SOAP綁定(binding),使用底層協(xié)議交換信息。
? ? ?引用命名空間:using System.Runtime.Serialization.Formatters.Soap;
3、將對象序列化到XML文檔
? ? ? ?同SOAP也是保存成XML文件.但沒有其他額外信息。XML 序列化僅序列化public類型的字段(其他兩種類型能保存所有類型的字段),且不保持類型保真度,當您要提供或使用數據而不限制使用該數據的應用程序時,這一點是很有用的。由于 XML 是一個開放式標準,因此,對于通過 Web 共享數據而言,這是一個很好的選擇。SOAP 同樣是一個開放式標準,這使它也成為一個頗具吸引力的選擇。
? ? ?引用命名空間:using System.Xml.Serialization;
三.C#中的三種序列化代碼示例
using System; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary;//二進制序列化 using System.Runtime.Serialization.Formatters.Soap;//SOAP序列化 using System.Xml.Serialization;//XML序列化 namespace WindowsFormsApplication1 { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? //如果要想保存某個class中的字段,必須在class前面加個這樣[Serializable] ? ? ? ? [Serializable] ? ? ? ? public class Person ? ? ? ? { ? ? ? ? ? ? public int age; ? ? ? ? ? ? public string name; ? ? ? ? ? ? [NonSerialized] //如果某個字段不想被保存,則加個這樣的標志 ? ? ? ? ? ? public string secret; ? ? ? ? } ? ? ? ? //序列化 ? ? ? ? private void btnXlh_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Person person = new Person(); ? ? ? ? ? ? person.age = 28; ? ? ? ? ? ? person.name = "Hoam\r\n"; ? ? ? ? ? ? person.secret = "no look"; ? ? ? ? ? ? string str = System.AppDomain.CurrentDomain.BaseDirectory; ? ? ? ? ? ? //二進制序列化 ? ? ? ? ? ? FileStream streamHx = new FileStream(str+"personHx.txt", FileMode.Create); ? ? ? ? ? ? BinaryFormatter bFormatHx = new BinaryFormatter(); ? ? ? ? ? ? bFormatHx.Serialize(streamHx, person); ? ? ? ? ? ? streamHx.Close(); ? ? ? ? ? ? //SOAP序列化 ? ? ? ? ? ? FileStream streamSoap = new FileStream(str + "personSoap.xml", FileMode.Create); ? ? ? ? ? ? SoapFormatter bFormatSoap = new SoapFormatter(); ? ? ? ? ? ? bFormatSoap.Serialize(streamSoap, person); ? ? ? ? ? ? streamSoap.Close(); ? ? ? ? ? ? //XML序列化 ?//某個字段加[NonSerialized]標志不起作用,仍被保存 ? ? ? ? ? ? FileStream streamXml = new FileStream(str+ "personXml.xml", FileMode.Create); ? ? ? ? ? ? XmlSerializer xmlserilize = new XmlSerializer(typeof(Person)); ? ? ? ? ? ? xmlserilize.Serialize(streamXml, person); ? ? ? ? ? ? streamXml.Close(); ? ? ? ? } ? ? ? ? //反序列化 ? ? ? ? private void btnFxlh_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Person person = new Person(); ? ? ? ? ? ? string str = System.AppDomain.CurrentDomain.BaseDirectory; ? ? ? ? ? ? object data; ? ? ? ? ? ? //二進制反序列化 ? ? ? ? ? ? FileStream streamHx = new FileStream(str + "personHx.txt", FileMode.OpenOrCreate); ? ? ? ? ? ? BinaryFormatter bFormatHx = new BinaryFormatter(); ? ? ? ? ? ? data = bFormatHx.Deserialize(streamHx); //反序列化得到的是一個object對象.須做下類型轉換 ? ? ? ? ? ? streamHx.Close(); ? ? ? ? ? ? //SOAP反序列化 ? ? ? ? ? ? FileStream streamSoap = new FileStream(str + "personSoap.xml", FileMode.OpenOrCreate); ? ? ? ? ? ? SoapFormatter bFormatSoap = new SoapFormatter(); ? ? ? ? ? ? data = bFormatSoap.Deserialize(streamSoap); //反序列化得到的是一個object對象.須做下類型轉換 ? ? ? ? ? ? streamSoap.Close(); ? ? ? ? ? ? //XML反序列化? ? ? ? ? ? ? FileStream streamXml = new FileStream(str + "personXml.xml", FileMode.OpenOrCreate); ? ? ? ? ? ? XmlSerializer xmlserilize = new XmlSerializer(typeof(Person)); ? ? ? ? ? ? data = xmlserilize.Deserialize(streamXml); //反序列化得到的是一個object對象.須做下類型轉換 ? ? ? ? ? ? streamXml.Close(); ? ? ? ? } ? ? } }
原文鏈接:https://www.cnblogs.com/ayxj/archive/2022/03/13/16000691.html
相關推薦
- 2022-06-16 golang?gorm更新日志執(zhí)行SQL示例詳解_Golang
- 2023-07-04 JUC阻塞隊列BlockingQueue---LinkedBlockingQueue
- 2022-06-21 Android?Studio實現登錄界面功能_Android
- 2022-09-13 Python實現創(chuàng)建模塊的方法詳解_python
- 2022-09-22 描述Servlet生命周期,Servlet是線程安全的嗎? 為什么?
- 2023-02-10 一文教會你用Python實現pdf轉word_python
- 2022-04-23 Tooltip 組件:根據內容是否溢出判斷是否顯示 Tooltip
- 2022-10-02 C++?電話號碼的字母組合功能實現_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支