对象的属性、字段
# 1.xml
XmlSerializer xml=new XmlSerializer(typeof(Person))
use(FileStream fs=new FileStream("person.xml",FileMode.Create))
{
xml.Serialize(fz,p1)
}
Console.WriteLine("ok");
Console.ReadKey();
1
2
3
4
5
6
7
2
3
4
5
6
7
# 2.json
JavaScrtptSerializer jsSer=new JavaScrtptSerializer();
string msg=jsSer.Serialize(p1);
Console.WriteLine(msg);
Console.ReadKey();
1
2
3
4
2
3
4
# 3.二进制
# BinaryFormatter类
- void Serialize(Stream stream,object graph)
- object Deserialize(Stream stream)
# 4.xml练习
XML的读取
private void button1_Click(object sender, EventArgs e)
{
//读取xml
XDocument document = new XDocument();
document = XDocument.Load("xdoc.xml");
XElement rootelement = document.Root;
TreeNode rootnodes=treeView1.Nodes.Add(rootelement.Name.ToString());
loadxmltotrees(rootelement,rootnodes.Nodes);
}
private void loadxmltotrees(XElement rootelement,TreeNodeCollection treenodecollection)
{
foreach (XElement item in rootelement.Elements())
{
if (item.Elements().Count() == 0)
{
treenodecollection.Add(item.Name.ToString()).Nodes.Add(item.Value);
}
else
{
TreeNode node = treenodecollection.Add(item.Name.ToString());
loadxmltotrees(item, node.Nodes);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
XmlDocument document1 = new XmlDocument();
document1.Load("xdoc.xml");
XmlElement rootelement = document1.DocumentElement;
TreeNode rootnodes = treeView1.Nodes.Add(rootelement.Name.ToString());
loadxmldoctotrees(rootelement, rootnodes.Nodes);
}
private void loadxmldoctotrees(XmlElement rootelement, TreeNodeCollection treenodecollection)
{
foreach (XmlNode item in rootelement.ChildNodes)
{
if (item.NodeType == XmlNodeType.Element)
{
TreeNode node = treenodecollection.Add(item.Name.ToString());
loadxmldoctotrees((XmlElement)item, node.Nodes);
}
else if (item.NodeType==XmlNodeType.Text|item.NodeType==XmlNodeType.CDATA)
{
treenodecollection.Add(item.InnerText);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
XmlDocument document1 = new XmlDocument();
document1.Load("xdoc.xml");
//XmlNodeList nodelist = document1.GetElementsByTagName("name");
//foreach (XmlNode node in nodelist)
//{
// treeView1.Nodes.Add(node.Name.ToString());
//}
//===============================
//XmlElement nodelist = document1.GetElementById("1");
//treeView1.Nodes.Add(nodelist.Name.ToString());
//=======================
XmlNodeList nodelist = document1.SelectNodes("list");//循环搜索
foreach (XmlNode node in nodelist)
{
treeView1.Nodes.Add(node.Name.ToString());
}
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.Filter = "*.xml|*.XML";
if(ofd1.ShowDialog()==DialogResult.OK)
{
string fullpath = ofd1.FileName;
XDocument document = XDocument.Load(fullpath);
XElement root = document.Root;
IEnumerable<XElement> ie= root.Descendants("person").Where(x=>Convert.ToInt32(x.Attribute("id").Value)>2);
foreach (var item in ie)
{
treeView1.Nodes.Add(item.Name.ToString());
}
}
}
xml的写入
private void button1_Click(object sender, EventArgs e)
{
//
XmlDocument xmldoc = new XmlDocument();
//增加文档说明
XmlDeclaration xmldeclaration = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmldoc.AppendChild(xmldeclaration);
//root根
XmlElement rootelement = xmldoc.CreateElement("school");
xmldoc.AppendChild(rootelement);
//class
XmlElement rootclass = xmldoc.CreateElement("class");
XmlAttribute attr=xmldoc.CreateAttribute("id");
attr.Value = "c01";
rootclass.Attributes.Append(attr);
rootelement.AppendChild(rootclass);
//student
XmlElement rootstudent = xmldoc.CreateElement("student");
XmlAttribute attrstd = xmldoc.CreateAttribute("sid");
attrstd.Value = "s011";
rootstudent.Attributes.Append(attrstd);
rootclass.AppendChild(rootstudent);
//
XmlElement xmlname = xmldoc.CreateElement("name");
xmlname.InnerText = "玉敏俊";
rootstudent.AppendChild(xmlname);
XmlElement xmlname1 = xmldoc.CreateElement("name");
xmlname1.InnerText = "玉敏俊";
rootstudent.AppendChild(xmlname1);
XmlElement xmlage = xmldoc.CreateElement("age");
xmlage.InnerText = "18";
rootstudent.AppendChild(xmlage);
//保存
xmldoc.Save("school.xml");
MessageBox.Show("ok");
}
private void button2_Click(object sender, EventArgs e)
{
List<person> list = new List<person>();
list.Add(new person() { name = "测试",age=19});
list.Add(new person() { name = "测试1", age = 20 });
list.Add(new person() { name = "测试2", age = 21 });
list.Add(new person() { name = "测试3", age = 22 });
XmlDocument xml = new XmlDocument();
XmlDeclaration xmldec = xml.CreateXmlDeclaration("1.0","utf-8","yes");
xml.AppendChild(xmldec);
XmlElement xmlroot = xml.CreateElement("list");
xml.AppendChild(xmlroot);
for (int i=0;i<list.Count(); i++)
{
XmlElement xmlperson = xml.CreateElement("person");
XmlAttribute xmlattrid = xml.CreateAttribute("id");
xmlattrid.Value = (i + 1).ToString();
xmlperson.Attributes.Append(xmlattrid);
xmlroot.AppendChild(xmlperson);
XmlElement xmlname = xml.CreateElement("name");
xmlname.InnerText=list[i].name;
xmlperson.AppendChild(xmlname);
XmlElement xmlage = xml.CreateElement("age");
xmlage.InnerText = list[i].age.ToString();
xmlperson.AppendChild(xmlage);
}
xml.Save("ceshi.xml");
MessageBox.Show("OK");
}
private void button3_Click(object sender, EventArgs e)
{
List<person> list = new List<person>();
list.Add(new person() { name = "测试", age = 19 });
list.Add(new person() { name = "测试1", age = 20 });
list.Add(new person() { name = "测试2", age = 21 });
list.Add(new person() { name = "测试3", age = 22 });
XDocument xdoc = new XDocument();
XDeclaration xdes = new XDeclaration("1.0", "utf-8", "yes");
XElement root = new XElement("list");
xdoc.Add(root);
for (int i = 0; i < list.Count; i++)
{
XElement xelementperson = new XElement("person");
xelementperson.SetAttributeValue("id", (i + 1).ToString());
root.Add(xelementperson);
xelementperson.SetElementValue("name",list[i].name);
xelementperson.SetElementValue("age", list[i].age.ToString());
}
xdoc.Save("xdoc.xml");
MessageBox.Show("OK");
}
}
public class person
{
public person()
{ }
public string name { get; set; }
public int age { get; set; }
}
xml练习
public class person
{
public person()
{ }
public person(string id,string name, string age, string Email)
{
this.id = id;
this.name = name;
this.age = age;
this.Email = Email;
}
public string id { get; set; }
public string name { get; set; }
public string age { get; set; }
public string Email { get; set; }
public override string ToString()
{
return this.name;
}
}
Dictionary<string, person> dict = new Dictionary<string, person>();
private void button1_Click(object sender, EventArgs e)
{
person model = new person(textBox1.Text.Trim(), textBox2.Text.Trim(),textBox3.Text.Trim(), textBox4.Text.Trim());
//
if (dict.ContainsKey(textBox1.Text.Trim()))
{
//已经包含
dict[model.id] = model;
person current = listBox1.SelectedItem as person;
if (current!=null)
{
current.id = model.id;
current.name = model.name;
current.age = model.age;
current.Email = model.Email;
}
listBox1.Items[listBox1.SelectedIndex] = model; //页面刷新关键的一句
}
else
{
dict.Add(model.id, model);
listBox1.Items.Add(model);
}
}
private void cleartextbox()
{
foreach (Control item in this.Controls)
{
if (item is TextBox)
{
((TextBox)item).Text = string.Empty;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
XDocument document = new XDocument();
XElement root = new XElement("personlist");
document.Add(root);
foreach (KeyValuePair<string,person> item in dict)
{
XElement personelement = new XElement("person");
root.Add(personelement);
personelement.SetElementValue("id",item.Value.id);
personelement.SetElementValue("name", item.Value.name);
personelement.SetElementValue("age", item.Value.age);
personelement.SetElementValue("Email", item.Value.Email);
}
document.Save("xml.xml");
MessageBox.Show("ok");
}
private void Form1_Load(object sender, EventArgs e)
{
XDocument document = XDocument.Load("xml.xml");
XElement root = document.Root;
foreach (XElement item in root.Elements("person"))
{
string id = item.Element("id").Value;
string name = item.Element("name").Value;
string age = item.Element("age").Value;
string Email = item.Element("Email").Value;
person model = new person(id,name,age,Email);
if (!dict.ContainsKey(model.id))
{
dict.Add(model.id,model);
//
listBox1.Items.Add(model);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
person p = listBox1.SelectedItem as person;
if (p!=null)
{
textBox1.Text = p.id;
textBox2.Text = p.name;
textBox3.Text = p.age;
textBox4.Text = p.Email;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# 图片
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (ofd1.ShowDialog()==DialogResult.OK)
{
string fullpath = ofd1.FileName;
Image im = Image.FromFile(fullpath);
pictureBox1.Image = im;
}
if (pictureBox1.Image!=null)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
byte[] imagebtye = ms.GetBuffer();
//MySqlConnection conn = new MySqlConnection(config.CONNECT_196);
//conn.Open();
string str = "insert into ymj.tb_pictrue (pictrue) values(@imageByte)";
//MySqlCommand comm = new MySqlCommand();
//comm.Connection = conn;
//comm.CommandText = str;
//comm.CommandType = CommandType.Text;
//comm.Parameters.Add(new MySqlParameter("?imageByte", MySqlDbType.MediumBlob)).Value = imagebtye;
//comm.ExecuteNonQuery();
MySqlParameter sp = new MySqlParameter("@imageByte",imagebtye);
MySqlHelper.ExecuteDataset(config.CONNECT_196,str,sp);
MessageBox.Show("成功");
}
//OpenFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
//if (OpenFileDialog1.ShowDialog()==DialogResult.OK)
//{
// fullpath = OpenFileDialog1.FileName;
// Bitmap bmp = new Bitmap(fullpath);
// MemoryStream ms = new MemoryStream();
// bmp.Save(ms,ImageFormat.Gif);
// byte[] arr = new byte[ms.Length];
// string pic = Convert.ToBase64String(arr);
// MySqlHelper.ExecuteNonQuery(config.CONNECT_196,sql);
//}
}
/// <summary>
/// 读取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string str = "select pictrue from ymj.tb_pictrue order by id desc";
MySqlDataReader dr=MySqlHelper.ExecuteReader(config.CONNECT_196,str);
if (dr.Read())
{
byte[] imagebyte = new byte[dr.GetBytes(0, 0, null, 0, int.MaxValue)];
dr.GetBytes(0, 0, imagebyte, 0, imagebyte.Length);
MemoryStream imagems = new MemoryStream(imagebyte);
Image image = Image.FromStream(imagems,true);
pictureBox1.Image = image;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63