您好,登錄后才能下訂單哦!
這篇文章給大家介紹C#中怎么利用XML實(shí)現(xiàn)序列化,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
序列化是將一個(gè)對(duì)象轉(zhuǎn)換成字節(jié)流以達(dá)到將其長(zhǎng)期保存在內(nèi)存、數(shù)據(jù)庫或文件中的處理過程。它的主要目的是保存對(duì)象的狀態(tài)以便以后需要的時(shí)候使用。與其相反的過程叫做反序列化。
序列化一個(gè)對(duì)象
為了序列化一個(gè)對(duì)象,我們需要一個(gè)被序列化的對(duì)象,一個(gè)容納被序列化了的對(duì)象的(字節(jié))流和一個(gè)格式化器。進(jìn)行序列化之前我們先看看System.Runtime.Serialization名字空間。ISerializable接口允許我們使任何類成為可序列化的類。
如果我們給自己寫的類標(biāo)識(shí)[Serializable]特性,我們就能將這些類序列化。除非類的成員標(biāo)記了[NonSerializable],序列化會(huì)將類中的所有成員都序列化。
序列化的類型
二進(jìn)制(流)序列化
SOAP序列化(使用IXmlSerializable)
XML序列化
通常大部分都是使用的XML序列化,所以介紹一下使用XML序列化.
Student類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XmlSerializers
{
[Serializable]
public class Student
{
private string name;
public string Name { get { return name; } set { name = value; } } private int age; public int Age { get { return age; } set { age = value; } } private string hoddy; public string Hoddy { get { return hoddy; } set { hoddy = value; } } public Student() { } public Student(string name,int age,string hoddy) { this.Name = name; this.Age = age; this.Hoddy = hoddy; } public void SayHi() { string mess = string.Format("我是{0},年齡有{1},愛好是{2}", Name, Age, Hoddy); Console.WriteLine(mess); } }
}
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace XmlSerializers
{
[Serializable]
class Program
{
static List<Student> students = new List<Student>(); static void Main(string[] args) { //添加學(xué)生 InitStudent(); //序列化 Serialize(); //反序列化 Deserialize(); Console.ReadLine(); } public static void InitStudent() { Student scoficedld = new Student("scofield", 28, "哈哈"); Student su = new Student("程沐喆", 34, "寫博客"); Student zhang = new Student("張婧", 21, "唱歌"); Student huang = new Student("黃飛鴻", 25, "打架"); Student ding = new Student("丁俊暉", 30, "打斯諾克"); Student sullivan = new Student("OSullivan", 33, "打147"); Student Jay = new Student("周杰", 21, "耍雙節(jié)棍"); students.Add(scoficedld); students.Add(su); students.Add(zhang); students.Add(huang); students.Add(ding); students.Add(sullivan); students.Add(Jay); } public static void Serialize() { FileStream fs = new FileStream("Serialize.xml", FileMode.Create); XmlSerializer xs = new XmlSerializer(typeof(List<Student>)); xs.Serialize(fs, students); fs.Close(); } public static void Deserialize() { FileStream fs = new FileStream("Serialize.xml", FileMode.Open); XmlSerializer xs = new XmlSerializer(typeof(List<Student>)); List<Student> lists = xs.Deserialize(fs) as List<Student>; if (lists != null) { for (int i = 0; i < lists.Count; i++) { lists[i].SayHi(); } } fs.Close(); } }
關(guān)于C#中怎么利用XML實(shí)現(xiàn)序列化就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。