



版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
C#XML序列化與反序列化很不爽,只能改成了BinaryWriter和BinaryReader來讀寫,突然想到能不能用XML來序列化?于是在網(wǎng)上查了些資料便寫了些實踐性代碼,做些記錄,避免以后忘記。序列化對象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);反序列化對象XmlSerializerser二newXmlSerializer(typeof(Student));Studentstu=
ser.Deserialize(File.OpenRead(HC:\
\x.xmド))asStudent;對象數(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=”Pet\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);對象數(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)建一個可以別序列化的類型,這里我定義了一個結(jié)構體,當然你也可以定義成其他的類。將Dictionaryv>中的數(shù)據(jù)依次放進結(jié)構體以后就可以放入流中了。[XmlAttribute("Nameり]意思是將
這個字段作為xml的屬性,屬性
名跟在グ‘中[XmlElement("Value")]意思是將這個字段做為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);是這樣了,沒有太炫的地方,
Deserialize反序列化。真希望.Net
能集成Dirctionaryv>對象,那我們這些懶人就方便了。在需要序列化的隊伍中,數(shù)組是很常見的類型,其次就是圖片了序列化圖片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é)構體來保存圖片,
這里我還加了個圖片的名字,到
時候查找起來也方便ー些圖片反序列化XmlSenalizerser=newXmlSerializer(typeof(ImageStruct));ImageStructs=
(ImageStruct)ser.Deserialize(File.O
penRead(nc:\\x.xmln));pictureBox1.Image二Image.FromStream(newMemoryStream(s.picture));memorystream來做緩存,這樣會比較快ー點,實際上我并沒有怎么感覺。圖片數(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年子宮收縮藥合作協(xié)議書
- 2025年特種銅合金材料合作協(xié)議書
- 2025年外賣騎手項目合作計劃書
- 2025年專門用途燈具:工藝裝飾燈具項目建議書
- 智慧城市公共服務信息安全的保障策略
- 汽車工程師的技能進階與行業(yè)趨勢
- 商業(yè)培訓中的適應性教育與個性化學習實踐
- 智慧城市基礎設施的能源管理技術發(fā)展趨勢
- 智能科技如何為地方教育事業(yè)注入新活力
- 中職數(shù)學橢圓課件
- 2024年 北京市公務員考試(行測)考試真題試題(附答案)
- 既有建筑地基基礎加固技術規(guī)范 JGJ 123-2012知識培訓
- 2025至2030中國改裝車行業(yè)深度發(fā)展研究與企業(yè)投資戰(zhàn)略規(guī)劃報告
- 中醫(yī)執(zhí)業(yè)醫(yī)師歷年真題及解答
- MT/T 1222-2024液壓支架再制造工程設計指南
- 2025年7月浙江省普通高中學業(yè)水平考試歷史仿真模擬卷01(含答案)
- 2024-2025學年人教版PEP六年級下學期期末試卷(含答案含聽力原文無音頻)
- 2025-2030年中國聚脲涂料行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 一級建造師考試安全管理試題及答案
- 《成人糖尿病患者的高血糖危象:共識報告》-學習與應用
- 遵義社工面試真題及答案
評論
0/150
提交評論