序列化與反序列化

2022-02-14 07:23:12 字數 3695 閱讀 3265

建立man.cs

using

system.xml.serialization;

//////

man

的摘要說明

///[serializable]

public

class

man[xmlelement("man_age")]

public

int age

public

string name

public

string tel

public man(int age, string name ,string tel)

public

string show()

age: tel:", this.name, this.age,this.tel);

}public

void adddata(int age, string name, string tel)

}1.二進位制格式序列化

首先引入

using

system.runtime.serialization.formatters.binary;

然後通過

string

path = "c:\\man.text";

man m=new

man (18,"

小王1"

,"18618618618");

filestream fs = new

filestream(path, filemode.create);

binaryformatter formatter = new

binaryformatter();

formatter.serialize(fs,m);

fs.close();

二進位制序列化反序列化

string

path = "c:\\man.text";

man m = null;

filestream fs = new

filestream(path,filemode.open);

binaryformatter formatter = new

binaryformatter();

m = formatter.deserialize(fs) as

man ;//

fs.close();

return  m.show();

2.xml序列化

string

path = "c:\\man.xml";

man m = new

man();

m.adddata(18, "

小王333"

, "18618618618");

using (streamwriter fs = new

streamwriter (path))

xml反序列化

string path = "c:\\man.xml";

man m = new

man ();

filestream fs = new

filestream(path,filemode.open);

xmlserializer xmlserializer = new

xmlserializer(m.gettype());

m = xmlserializer.deserialize(fs) as

man ;//

fs.close();

return m.show();

插曲:  可以通過       [nonserialized]  對物件中的的部分資料取消序列化 做到部分暴露

如果宣告乙個類 而沒有共有字段 那麼這個類進行xml序列化的時候  將無法把資料寫入xml檔案

public

class

xmlserializehelp

}public

static t deserialize(string path) where t:class

}

反序列化輸出xml:

反序列化基類:

public

class

baseapi : system.web.ui.page

}//////

序列化

/// ///

型別 ///

物件 ///

public

static

string serializer(object

obj,type type)

catch

(invalidoperationexception)

stream.position = 0

; streamreader sr = new

streamreader(stream);

string str =sr.readtoend();

sr.dispose();

stream.dispose();

return

str;

}}

定義 輸出xml格式物件,同時指定字段  輸出 格式

[serializable]

public

class

news

public

int id

public

string title

[xmlignore]

public

string content

[xmlelement(

"text_content")]

public

xmlnode text_content

set }

}

輸出:

public

partial

class

news_api : baseapi

}public

void get_content(int

id)

}

備註:調整了基類方法寫法:

///

///反序列化

/// ///

//////

public

static t deserialize(string strxml) where t:class

}//////

序列化

/// ///

型別 ///

物件 ///

public

static

string serializer(object

obj)

catch

(invalidoperationexception)

stream.position = 0

; streamreader sr = new

streamreader(stream);

string str =sr.readtoend();

sr.dispose();

stream.dispose();

return

str;

}

序列化和反序列化 C 序列化與反序列化。

序列化介紹 把物件用一種新的格式來表示。系列化只序列化資料。序列化不建議使用自動屬性 為什麼要序列化 將乙個複雜的物件轉換流,方便儲存與資訊交換。class program class person public int age 二進位制序列化 就是將物件變成流的過程,把物件變成byte class...

序列化與反序列化

把複雜的資料型別壓縮到乙個字串中 serialize 把變數和它們的值編碼成文字形式 unserialize 恢復原先變數 eg stooges array moe larry curly new serialize stooges print r new echo print r unserial...

序列化與反序列化

序列化是將物件處理為位元組流以儲存物件或傳輸到記憶體 資料庫或檔案。其主要目的是儲存物件的狀態,以便可以在需要時重新建立物件。相反的過程稱為反序列化。通過序列化,開發人員可以儲存物件的狀態,並在需要時重新建立該物件,從而提供物件的儲存以及資料交換。通過序列化,開發人員還可以執行類似如下的操作 通過 ...