網(wǎng)站首頁 編程語言 正文
序列化和反序列化是指什么?
序列化(seriallization): 將對(duì)象轉(zhuǎn)化為便于傳輸?shù)臄?shù)據(jù)格式, 常見的序列化格式:二進(jìn)制格式,字節(jié)數(shù)組,json字符串,xml字符串。
反序列化(deseriallization):將序列化的數(shù)據(jù)恢復(fù)為對(duì)象的過程。
XmlSerializer類
該類用一種高度松散耦合的方式提供串行化服務(wù)。你的類不需要繼承特別的基類,而且它們也不需要實(shí)現(xiàn)特別的接口。相反,你只需在你的類或者這些類的公共域以及讀/寫屬性里加上自定義的特性。XmlSerializer通過反射機(jī)制讀取這些特性并用它們將你的類和類成員映射到xml元素和屬性(在對(duì)象和 XML 文檔之間進(jìn)行序列化和反序列化操作)。
.NET-XML序列化和反序列化簡單示例代碼
using System.IO; using System.Xml.Serialization; namespace Practices.Common { public class SimpleSerializer { ////// Model實(shí)體對(duì)象序列化為XML字符串 /// ///對(duì)象類型 /// Model實(shí)體對(duì)象 ///public static string SerializeXMLL (T t) { using (StringWriter sw = new StringWriter()) { XmlSerializer xmlSerializer = new XmlSerializer(t.GetType()); xmlSerializer.Serialize(sw, t); return sw.ToString(); } } /// /// XML反序列化為對(duì)象 /// ///對(duì)象類型 /// xml字符串 ///public static T Deserialize (string xml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); StringReader stringReader = new StringReader(xml); return (T)xmlSerializer.Deserialize(stringReader); } } }
XmlTypeAttribute類
?該類主要控制當(dāng)屬性目標(biāo)由XML序列化時(shí)生成的XML節(jié)點(diǎn)。
應(yīng)用示例
////// Envelope /// [XmlType(TypeName = "envelope")] public class CityRes { public Header header { get; set; } public Response response { get; set; } }
XmlElementAttribute類
? 該類用于指示公共字段或?qū)傩栽赬ML序列化或反序列化包含它們的對(duì)象時(shí)表示XML元素。
應(yīng)用示例
////// Envelope /// public class CityRes { ////// header /// [XmlElement("header")] public Header header { get; set; } ////// response /// [XmlElement("response")] public Response response { get; set; } }
XmlAttributeAttribute類
該類指定XML序列化必須將類成員序列化為XML屬性。
應(yīng)用示例
////// Version /// public class Version { ////// port /// [XmlAttribute("port")] public string port { get; set; } ////// host /// [XmlAttribute("host")] public string host { get; set; } ////// text /// [XmlAttribute("text")] public string text { get; set; } }
XmlArrayAttribute類
該類主要用于XML元素?cái)?shù)組的應(yīng)用(相當(dāng)于就是集合的聲明)。
應(yīng)用示例
[XmlArray] public Item []Items { get{return items;} set{items = value;} }
XmlTextAttribute類
當(dāng)Xml文檔序列化或反序列化時(shí)使用該特性修飾的成員會(huì)作為XML文本處理。
應(yīng)用示例
[System.Xml.Serialization.XmlTextAttribute()] public string[] Text { get { return this.textField; } set { this.textField = value; } }
XmlIgnoreAttribute類
? 指示該特性修飾的對(duì)象在Xml序列化時(shí)不會(huì)序列化該特性指定的元素。
應(yīng)用示例
public class TeamGroup { [XmlIgnore] public string Comment; public string GroupName; }
用來控制XML序列化的屬性匯總
通過將下表中的特性應(yīng)用于類和類成員,可以控制?XmlSerializer?序列化或反序列化該類的實(shí)例的方式。 若要了解這些屬性如何控制 XML 序列化,請(qǐng)參閱使用屬性控制 XML 序列化。
特性 | 適用對(duì)象 | 指定 |
---|---|---|
XmlAnyAttributeAttribute | 公共字段、屬性、參數(shù)或返回?XmlAttribute?對(duì)象數(shù)組的返回值。 | 反序列化時(shí),將會(huì)使用?XmlAttribute?對(duì)象填充數(shù)組,而這些對(duì)象代表對(duì)于架構(gòu)未知的所有 XML 特性。 |
XmlAnyElementAttribute | 公共字段、屬性、參數(shù)或返回?XmlElement?對(duì)象數(shù)組的返回值。 | 反序列化時(shí),將會(huì)使用?XmlElement?對(duì)象填充數(shù)組,而這些對(duì)象代表對(duì)于架構(gòu)未知的所有 XML 元素。 |
XmlArrayAttribute | 公共字段、屬性、參數(shù)或返回復(fù)雜對(duì)象的數(shù)組的返回值。 | 數(shù)組成員將作為 XML 數(shù)組的成員生成。 |
XmlArrayItemAttribute | 公共字段、屬性、參數(shù)或返回復(fù)雜對(duì)象的數(shù)組的返回值。 | 可以插入數(shù)組的派生類型。 通常與?XmlArrayAttribute?一起應(yīng)用。 |
XmlAttributeAttribute | 公共字段、屬性、參數(shù)或返回值。 | 成員將作為 XML 屬性進(jìn)行序列化。 |
XmlChoiceIdentifierAttribute | 公共字段、屬性、參數(shù)或返回值。 | 可以使用枚舉進(jìn)一步消除成員的歧義。 |
XmlElementAttribute | 公共字段、屬性、參數(shù)或返回值。 | 字段或?qū)傩詫⒆鳛?XML 元素進(jìn)行序列化。 |
XmlEnumAttribute | 作為枚舉標(biāo)識(shí)符的公共字段。 | 枚舉成員的元素名稱。 |
XmlIgnoreAttribute | 公共屬性和公共字段。 | 序列化包含類時(shí),應(yīng)該忽略屬性或字段。 |
XmlIncludeAttribute | 公共派生類聲明,以及 Web 服務(wù)描述語言 (WSDL) 文檔的公共方法的返回值。 | 生成要在序列化時(shí)識(shí)別的架構(gòu)時(shí),應(yīng)該將該類包括在內(nèi)。 |
XmlRootAttribute | 公共類聲明。 | 控制視為 XML 根元素的屬性目標(biāo)的 XML 序列化。 使用該屬性可進(jìn)一步指定命名空間和元素名稱。 |
XmlTextAttribute | 公共屬性和公共字段。 | 屬性或字段應(yīng)該作為 XML 文本進(jìn)行序列化。 |
XmlTypeAttribute | 公共類聲明。 | XML 類型的名稱和命名空間。 |
參考文章
C#: .net序列化及反序列化
用來控制XML序列化的屬性
使用屬性控制XML序列化
微軟官方文檔-XML相關(guān)類匯總
原文鏈接:https://www.cnblogs.com/Can-daydayup/p/16052873.html
相關(guān)推薦
- 2023-06-04 Pandas中MultiIndex選擇并提取任何行和列_python
- 2022-04-19 在html中src和href的區(qū)別,以及img中的srcset的作用是什么?
- 2022-03-19 好用的C++?string?Format“函數(shù)”介紹_C 語言
- 2023-01-07 Flutter?Widget開發(fā)Shortcuts快捷鍵實(shí)例_Android
- 2022-04-11 css實(shí)現(xiàn)左邊div固定寬度,右邊div自適應(yīng)撐滿剩下的寬度
- 2022-10-11 python嵌套try...except如何使用詳解_python
- 2023-12-06 Access denied for user root @ localhost (using p
- 2021-11-08 Linux常用硬盤管理相關(guān)命令介紹_Linux
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支