序列化类
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
|
using
System;
using
System.Collections.Generic;
using
System.IO;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Xml.Serialization;
namespace
ConsoleApplication31
{
class
Program
{
static
void
Main(
string
[] args)
{
Student stu =
new
Student()
{
Age = 10,
Class =
"Class One"
,
Name =
"Tom"
,
Number = 1
};
XmlSerializer ser =
new
XmlSerializer(
typeof
(Student));
ser.Serialize(File.Create(
"D:\\temp\\x.xml"
), stu);
}
}
public
class
People
{
[XmlAttribute(
"NAME"
)]
public
string
Name
{
set
;
get
; }
[XmlAttribute(
"AGE"
)]
public
int
Age
{
set
;
get
; }
}
[XmlRoot(
"Root"
)]
public
class
Student : People
{
[XmlElement(
"CLASS"
)]
public
string
Class
{
set
;
get
; }
[XmlElement(
"NUMBER"
)]
public
int
Number
{
set
;
get
; }
}
}
|
序列化List类
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
|
using
System;
using
System.Collections.Generic;
using
System.IO;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Xml.Serialization;
namespace
ConsoleApplication31
{
class
Program
{
static
void
Main(
string
[] args)
{
List<Student> ls =
new
List<Student>();
Student stu1 =
new
Student()
{
Age = 10,
Class =
"Class One"
,
Name =
"Tom"
,
Number = 1
};
Student stu2 =
new
Student()
{
Age = 20,
Class =
"Class Two"
,
Name =
"Daniel"
,
Number = 2
};
ls.Add(stu1);
ls.Add(stu2);
XmlSerializer ser =
new
XmlSerializer(
typeof
(List<Student>));
ser.Serialize(File.Create(
"D:\\temp\\listx.xml"
), ls);
}
}
public
class
People
{
[XmlAttribute(
"NAME"
)]
public
string
Name
{
set
;
get
; }
[XmlAttribute(
"AGE"
)]
public
int
Age
{
set
;
get
; }
}
[XmlRoot(
"Root"
)]
public
class
Student : People
{
[XmlElement(
"CLASS"
)]
public
string
Class
{
set
;
get
; }
[XmlElement(
"NUMBER"
)]
public
int
Number
{
set
;
get
; }
}
}
|
参考链接:
http://blog.csdn.net/wem520/article/details/9192415
本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1929564,如需转载请自行联系原作者