資源描述:
《c#XML序列化與反序列化》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在行業(yè)資料-天天文庫。
C#XML序列化與反序列化
1很不爽,只能改成了BinaryWriter和BinaryReader來讀寫,突然想到能不能用XML來序列化?于是在網(wǎng)上查了些資料便寫了些實踐性代碼,做些記錄,避免以后忘記。
2序列化對象
3publicclassPeople
4
5[XmlAttribute(nNAMEn)]
6publicstringName
7{set;get;}
8[XmlAttribute(nAGEn)]
9publicintAge
10{set;get;}
11[XmlRoot(nRootn)]
12publicclassStudent:People
13
14[XmlElement(nCLASSn)]
15publicstringClass
16{set;get;}
17[XmlElement(nNUMBERn)]
18publicintNumber
19{set;get;}
20voidMain(string[]args)
21
22Studentstu=newStudent()
23r\〇
24Class二''ClassOne"
25Name=''Tom"
26Number=1
27XmlSerializerser=newXmlSerializer(typeof(Student));
28ser.Serialize(File.Create(HC:\\x.xml”エstu);
29反序列化對象
30XmlSerializerser二newXmlSerializer(typeof(Student));
31Studentstu=ser.Deserialize(File.OpenRead(HC:\\x.xmド))asStudent;
32對象數(shù)組序列化
33publicclassPeople
34
35[XmlAttribute(nNAMEn)]
36publicstringName
37{set;get;}
38[XmlAttribute(nAGEn)]
39publicintAge
40{set;get;}
41[XmlRoot(nRootn)]
42publicclassStudent:People
43
44[XmlElement(nCLASSn)]
45publicstringClass
46{set;get;}
47[XmlElement(nNUMBERn)]
48publicintNumber
49{set;get;}
50voidMain(string[]args)
51
52ListstuListnewList();
53stuList.Add(newStudent(){Age=10,Number=1,Name=”Tom\Class二"ClassOnen});
54stuList.Add(newStudent(){Age=11,Number=2,Name=叮ay",Class二"ClassTwo"});
55stuList.Add(newStudent(){Age=12,Number=3,Name=”Pet\Class二"ClassOnen});
56stuList.Add(newStudent(){Age=13,Number=4,Name=“May",Class=nClassThreen});
57stuList.Add(newStudent(){Age=14,Number=5,Name=“Soy",Class二"ClassTwo"});
58XmlSerializerser=newXmlSerializer(typeof(List));
59ser.Serialize(File.Create(HC:\\x.xmlり,stuList);
60對象數(shù)組反序列
61XmlSerializerser=newXmlSerializer(typeof(List));
62ListstuList=ser.Deserialize(File.OpenRead(HC:\\x.xmド))asList;
63fOreach(StudentsinstuList)
64MessageBox.Show(string.Format(n{。}:{1}:{2}:{3}ゝ
65s.Name,s.Age,s.Class,s.Number));
66序列化Dirctionary
67publicstructDirectionList
68
69[XmlAttribute("Namen)]
70publicstringName;
71[XmlElement(nValuen)]
72publicintValue;
73voidMain(string[]args)
74
75Dictionarylist=newDictionary();
76list.Add(nr,,100);
77list.AddC'2\200);
78list.Add(n3n,300);
79list.Add(n4n,400);
80hstAddCS11,500);
81list.Add(n6n,600);
82list.Add(n7n,700);
83,(008:⑻Jppvisr
84list.AddC'9'[900);
85ListdirListnewList();
86foreach(varsinlist)
87dirList.Add(newDirectionList(){Name=s.Key,Value=s.Value});
88XmlSerializerser二newXmlSerializer(typeof(List));
89ser.Serialize(File.Create(nC:\\x.xmln),dirList);
901-1--Xイ/QIIIIHノーノ、^1=^T。L4Iソ、フ只能按照它的格式先創(chuàng)建一個可以別序列化的類型,這里我定義了一個結構體,當然你也可以定義成其他的類。將Dictionaryv>中的數(shù)據(jù)依次放進結構體以后就可以放入流中了。
91[XmlAttribute("Nameり]意思是將這個字段作為xml的屬性,屬性名跟在グ‘中
92[XmlElement("Value")]意思是將這個字段做為xml的元素。
93反序列化Dirctionary
94XmlSerializerser二newXmlSerializer(typeof(List));
95ListdirList=ser.Deserialize(
96File.OpenRead(nC:\\x.xmln))asList;
97foreach(varvindirList)
98Console.WriteLine(n{0}:{1}f\v.Name,v.Value);
99是這樣了,沒有太炫的地方,Deserialize反序列化。真希望.Net能集成Dirctionaryv>對象,那我們這些懶人就方便了。
100在需要序列化的隊伍中,數(shù)組是很常見的類型,其次就是圖片了
101序列化圖片
102publicstructImageStruct
103
104[XmlAttribute(''Numbern)]
105publicintnumber;
106[XmlElement(ffImageff)]
107publicbyte[]picture;
108voidMain(string[]args)
109
110ImageStruct(){number=1,picture=File.ReadAllBytes(@"H.jpg")};
111XmlSenalizerser=newXmlSerializer(typeof(ImageStruct));
112FileStreamfs=File.Create(nc:\\x.xmr');
113fs.CloseQ;
114ー樣的,采用結構體來保存圖片,這里我還加了個圖片的名字,到時候查找起來也方便ー些
115圖片反序列化
116XmlSenalizerser=newXmlSerializer(typeof(ImageStruct));
117ImageStructs=(ImageStruct)ser.Deserialize(File.OpenRead(nc:\\x.xmln));
118pictureBox1.Image二Image.FromStream(newMemoryStream(s.picture));
119memorystream來做緩存,這樣會比較快ー點,實際上我并沒有怎么感覺。
120圖片數(shù)組序列化
121publicstructImageStruct
122
123[XmlAttribute(''Numbern)]
124publicintnumber;
125[XmlElement(ffImageff)]
126publicbyte[]picture;
127voidMain(string[]args)
128
129ListimageList=newList();
130imageList.Add(newImageStruct()number=1,
131picture=File.ReadAllBytes(@”11.jpg”)
132?c
133imageList.Add(newImageStruct()
134
135picture=File.ReadAllBytes(@n22.jpgn)
136?c
137XmlSerializerser二newXmlSerializer(typeof(List));
138FileStreamfs=File.Create(nc:\\x.xmr');
139ser.Serialize(fs,imageList);
140fs.CloseQ;
141圖片數(shù)組反序列化
142XmlSerializerser二newXmlSerializer(typeof(List));
143Lists=(List)ser.Deserialize(File.OpenRead("c:\\x.xmド));
144varim=fromiins
145wherei.number==1
146selecti.picture;
147//varim=s.Where(p=>p.number==l).Select(p=>p.picture);
148foreach(varimageinim)
149pictureBox1.Image=Image.FromStream(
150newMemoryStream(image));
151這里還對數(shù)組結構進仃「Linq查詢,這樣就可以很方便的查詢圖片了。
152要序列化的對象的類:
153[Serializable]
154publicclassPerson
155
156privatestringname;
157publicstringName
158
159
160
161returnname;
162
163name=value;