



版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C#XML序列化與反序列化很不爽,只能改成了BinaryWriter和BinaryReader來(lái)讀寫(xiě),突然想到能不能用XML來(lái)序列化?于是在網(wǎng)上查了些資料便寫(xiě)了些實(shí)踐性代碼,做些記錄,避免以后忘記。序列化對(duì)象publicclassPeople[XmlAttribute(nNAMEn)]publicstringName{set;get;}[XmlAttribute(nAGEn)]publicintAge{set;get;}[XmlRoot(nRootn)]publicclassStudent:People[XmlElement(nCLASSn)]publicstringClass{set;get;}[XmlElement(nNUMBERn)]publicintNumber{set;get;}voidMain(string[]args)Studentstu=newStudent()r\〇Class二''ClassOne"Name=''Tom"Number=1XmlSerializerser=newXmlSerializer(typeof(Student));ser.Serialize(File.Create(HC:\\x.xml
”エstu);反序列化對(duì)象XmlSerializerser二newXmlSerializer(typeof(Student));Studentstu=
ser.Deserialize(File.OpenRead(HC:\
\x.xmド))asStudent;對(duì)象數(shù)組序列化publicclassPeople[XmlAttribute(nNAMEn)]publicstringName{set;get;}[XmlAttribute(nAGEn)]publicintAge{set;get;}[XmlRoot(nRootn)]publicclassStudent:People[XmlElement(nCLASSn)]publicstringClass{set;get;}[XmlElement(nNUMBERn)]publicintNumber{set;get;}voidMain(string[]args)List<Student>stuListnewList<Student>();stuList.Add(newStudent(){Age=10,Number=1,Name=”Tom\Class二"ClassOnen});stuList.Add(newStudent(){Age=11,Number=2,Name=叮ay",Class二"ClassTwo"});stuList.Add(newStudent(){Age=12,Number=3,Name=”P(pán)et\Class二"ClassOnen});stuList.Add(newStudent(){Age=13,Number=4,Name=“May",Class=nClassThreen});stuList.Add(newStudent(){Age=14,Number=5,Name=“Soy",Class二"ClassTwo"});XmlSerializerser=new
XmlSerializer(typeof(List<Student
>));ser.Serialize(File.Create(HC:\\x.xml
り,stuList);對(duì)象數(shù)組反序列XmlSerializerser=new
XmlSerializer(typeof(List<Student
>));List<Student>stuList=
ser.Deserialize(File.OpenRead(HC:\
\x.xmド))asList<Student>;fOreach(StudentsinstuList)MessageBox.Show(string.Format(n{。}:{1}:{2}:{3}ゝs.Name,s.Age,s.Class,s.Number));序列化DirctionarypublicstructDirectionList[XmlAttribute("Namen)]publicstringName;[XmlElement(nValuen)]publicintValue;voidMain(string[]args)Dictionary<string,int>list=newDictionary<string,int>();list.Add(nr,,100);list.AddC'2\200);list.Add(n3n,300);list.Add(n4n,400);hstAddCS11,500);list.Add(n6n,600);list.Add(n7n,700);,(008:⑻Jppvisrlist.AddC'9'[900);List<DirectionList>dirListnewList<DirectionList>();foreach(varsinlist)dirList.Add(new
DirectionList(){Name=s.Key,
Value=s.Value});XmlSerializerser二new
XmlSerializer(typeof(List<Directio
nList>));ser.Serialize(File.Create(nC:\\x.xml
n),dirList);1-1--Xイ/QI II IHノーノ、^1=^T。L4Iソ、フ只能按照它的格式先創(chuàng)建一個(gè)可以別序列化的類(lèi)型,這里我定義了一個(gè)結(jié)構(gòu)體,當(dāng)然你也可以定義成其他的類(lèi)。將Dictionaryv>中的數(shù)據(jù)依次放進(jìn)結(jié)構(gòu)體以后就可以放入流中了。[XmlAttribute("Nameり]意思是將
這個(gè)字段作為xml的屬性,屬性
名跟在グ‘中[XmlElement("Value")]意思是將這個(gè)字段做為xml的元素。反序列化DirctionaryXmlSerializerser二new
XmlSerializer(typeof(List<Directio
nList>));List<DirectionList>dirList=ser.Deserialize(File.OpenRead(nC:\\x.xmln))as
List<DirectionList>;foreach(varvindirList)Console.WriteLine(n{0}:{1}f\v.Name,v.Value);是這樣了,沒(méi)有太炫的地方,
Deserialize反序列化。真希望.Net
能集成Dirctionaryv>對(duì)象,那我們這些懶人就方便了。在需要序列化的隊(duì)伍中,數(shù)組是很常見(jiàn)的類(lèi)型,其次就是圖片了序列化圖片publicstructImageStruct[XmlAttribute(''Numbern)]publicintnumber;[XmlElement(ffImageff)]publicbyte[]picture;voidMain(string[]args)ImageStruct(){number=1,
picture=
File.ReadAllBytes(@"H.jpg")};XmlSenalizerser=newXmlSerializer(typeof(ImageStruct));FileStreamfs=File.Create(nc:\\x.xmr');fs.CloseQ;ー樣的,采用結(jié)構(gòu)體來(lái)保存圖片,
這里我還加了個(gè)圖片的名字,到
時(shí)候查找起來(lái)也方便ー些圖片反序列化XmlSenalizerser=newXmlSerializer(typeof(ImageStruct));ImageStructs=
(ImageStruct)ser.Deserialize(File.O
penRead(nc:\\x.xmln));pictureBox1.Image二Image.FromStream(newMemoryStream(s.picture));memorystream來(lái)做緩存,這樣會(huì)比較快ー點(diǎn),實(shí)際上我并沒(méi)有怎么感覺(jué)。圖片數(shù)組序列化publicstructImageStruct[XmlAttribute(''Numbern)]publicintnumber;[XmlElement(ffImageff)]publicbyte[]picture;voidMain(string[]args)List<ImageStruct>imageList=newList<ImageStruet>();imageList.Add(newImageStruct()number=1,picture=File.ReadAllBytes(@”11.jpg”)?cimageList.Add(newImageStruct()picture=File.ReadAllBytes(@n22.jpgn)?cXmlSerializerser二new
XmlSerializer(typeof(List<ImageSt
ruct>));FileStreamfs=File.Create(nc:\\x.xmr');ser.Serialize(fs,imageList);fs.CloseQ;圖片數(shù)組反序列化XmlSerializerser二new
XmlSerializer(typeof(List<ImageSt
ruct>));List<ImageStruct>s=(List<ImageStruct>)ser.Deserialize(File.OpenRead("c:\\x.xmド));varim=fromiinswherei.number==1selecti.picture;//varim=s.Where(p=>p.number==l).Select(p=>p.picture
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024面向醫(yī)學(xué)影像的多病癥人工智能診斷應(yīng)用平臺(tái)
- 2025陽(yáng)光電源光伏并網(wǎng)逆變器性能指標(biāo)技術(shù)規(guī)范
- 遠(yuǎn)程工作的全球化趨勢(shì)及影響
- 室內(nèi)設(shè)計(jì)裝修行業(yè)新年個(gè)人工作計(jì)劃
- 幼兒園角色教育實(shí)施方案計(jì)劃
- 跨境支付與金融管理在電商平臺(tái)的應(yīng)用
- 人員全息檔案搜索軟件需求
- 禮品行業(yè)的國(guó)際化市場(chǎng)拓展與創(chuàng)新策略
- 云南2024年12月云南省芒市2025年招考公益性崗位人員(第一批)筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 初中語(yǔ)文生活美文邱吉爾的選擇
- 假肢安裝合同范本
- DB37-T4824-2025 鄉(xiāng)鎮(zhèn)(街道)應(yīng)急物資配備指南
- 教育部人文社科 申請(qǐng)書(shū)
- 無(wú)菌手術(shù)臺(tái)鋪置的細(xì)節(jié)管理
- 《重大基礎(chǔ)設(shè)施項(xiàng)目涉及風(fēng)景名勝區(qū)選址論證報(bào)告編制技術(shù)規(guī)范》編制說(shuō)明
- 議論文8(試題+審題+范文+點(diǎn)評(píng)+素材)-2025年高考語(yǔ)文寫(xiě)作復(fù)習(xí)
- 2025-2030年(全新版)中國(guó)軟冰淇淋市場(chǎng)發(fā)展趨勢(shì)與投資戰(zhàn)略研究報(bào)告
- 2025新人教版英語(yǔ)七年級(jí)下單詞默寫(xiě)表(小學(xué)部分)
- 2024年大慶醫(yī)學(xué)高等專(zhuān)科學(xué)校高職單招語(yǔ)文歷年參考題庫(kù)含答案解析
- 四川省綿陽(yáng)市2025屆高三上學(xué)期第二次診斷性考試語(yǔ)文試題(含答案)
- 2025江蘇蘇州高新區(qū)獅山商務(wù)創(chuàng)新區(qū)下屬?lài)?guó)企業(yè)招聘9人高頻重點(diǎn)提升(共500題)附帶答案詳解
評(píng)論
0/150
提交評(píng)論