前台调用:
1
using System;
2
using Bmc;
3
4
public partial class _Default : System.Web.UI.Page
5
{
6
protected void btnSerializer_Click(object sender, EventArgs e)
7
{
8
Comment firstComment = new Comment();
9
firstComment.ID = 1;
10
firstComment.userName = "first";
11
Comment secondComment = new Comment();
12
secondComment.userName = "second";
13
secondComment.ID = 2;
14
15
CommentArray commentArray = new CommentArray();
16
commentArray.Set(firstComment);
17
commentArray.Set(secondComment);
18
19
System.Type type = typeof(CommentArray);
20
SerializaObject.Serializa(Server.MapPath("~/1.XML"), type, commentArray);
21
}
22
}
23
具体操作类:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.IO;
11
using System.Xml.Serialization;
12
13
/// <summary>
14
/// Summary description for Serializer
15
/// </summary>
16
public class SerializaObject
17
{
18
public SerializaObject()
19
{
20
//
21
// TODO: Add constructor logic here
22
//
23
}
24
/// <summary>
25
/// serilia
26
/// </summary>
27
/// <param name="filePath"></param>
28
/// <param name="objectType"></param>
29
/// <param name="nObject"></param>
30
/// <returns></returns>
31
public static bool Serializa(string filePath,Type objectType,object nObject)
32
{
33
FileStream fileStream = new FileStream(@filePath, FileMode.Create);
34
XmlSerializer serializer = new XmlSerializer(objectType);
35
try
36
{
37
serializer.Serialize(fileStream, nObject);
38
return true;
39
}
40
catch(Exception ex)
41
{
42
throw ex;
43
}
44
finally
45
{
46
fileStream.Close();
47
}
48
}
49
}
50

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

相关的实体类:
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Collections.Generic;
6
7
namespace Bmc
8
{
9
/// <summary>
10
/// 评论Comment类,主要方法,可以实现Comment对象的:添加、删除、查看
11
/// </summary>
12
[Serializable]
13
public class Comment
14
{
15
/// <summary>
16
/// Global_ID
17
/// </summary>
18
public int ID;
19
/// <summary>
20
/// 用户名或者昵称
21
/// </summary>
22
public string userName;
23
/// <summary>
24
/// 编辑时间
25
/// </summary>
26
public DateTime EditTime;
27
/// <summary>
28
/// 状态
29
/// </summary>
30
public int states;
31
/// <summary>
32
/// Normal
33
/// </summary>
34
public const int StatusNormal = 1;
35
/// <summary>
36
/// Deleted
37
/// </summary>
38
public const int StatusDeleted = 0;
39
/// <summary>
40
/// Table Name
41
/// </summary>
42
public const string tableName = "T_Comment";
43
/// <summary>
44
/// 初始化
45
/// </summary>
46
public Comment()
47
{
48
userName = "游客";
49
states = StatusNormal;
50
EditTime = System.DateTime.Now;
51
}
52
}
53
[Serializable]
54
public class CommentArray
55
{
56
/// <summary>
57
/// Comment Array
58
/// </summary>
59
public System.Collections.Generic.List<Comment> iArray;
60
/// <summary>
61
/// Initial
62
/// </summary>
63
public CommentArray()
64
{
65
iArray = new List<Comment>();
66
}
67
/// <summary>
68
/// Set Value
69
/// </summary>
70
/// <param name="iComment"></param>
71
public void Set(Comment iComment)
72
{
73
iArray.Add(iComment);
74
}
75
/// <summary>
76
/// Remove Value From Comment Array
77
/// </summary>
78
public void Remove(Comment iComment)
79
{
80
iArray.Remove(iComment);
81
}
82
}
83
84
}
运行结果: 注意:最开始的XML说明缺少了encoding="utf-8"?

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

1
<?xml version="1.0"?>
2
<CommentArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3
<iArray>
4
<Comment>
5
<ID>1</ID>
6
<userName>first</userName>
7
<EditTime>2007-11-07T18:27:33.15625+08:00</EditTime>
8
<states>1</states>
9
</Comment>
10
<Comment>
11
<ID>2</ID>
12
<userName>second</userName>
13
<EditTime>2007-11-07T18:27:33.15625+08:00</EditTime>
14
<states>1</states>
15
</Comment>
16
</iArray>
17
</CommentArray
改进办法:使用指定的 TextWriter 序列化指定的 Object 并将 XML 文档写入文件。 
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

1
/// <summary>
2
/// Serializa,这个加入了xml的编码规则UTF8
3
/// </summary>
4
/// <param name="filePath"></param>
5
/// <param name="objectType"></param>
6
/// <param name="nObject"></param>
7
/// <returns></returns>
8
public static bool Serializa(string filePath, Type objectType, object nObject)
9
{
10
FileStream fileStream = new FileStream(@filePath, FileMode.Create);
11
TextWriter writer = new StreamWriter(fileStream, new UTF8Encoding());
12
XmlSerializer serializer = new XmlSerializer(objectType);
13
try
14
{
15
serializer.Serialize(writer, nObject);
16
return true;
17
}
18
catch (Exception ex)
19
{
20
throw ex;
21
}
22
finally
23
{
24
writer.Close();
25
}
26
}

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

改进后的结果:
1
<?xml version="1.0" encoding="utf-8"?>
2
<CommentArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3
<iArray>
4
<Comment>
5
<ID>1</ID>
6
<userName>first</userName>
7
<EditTime>2007-11-07T18:55:41.9375+08:00</EditTime>
8
<states>1</states>
9
</Comment>
10
<Comment>
11
<ID>2</ID>
12
<userName>second</userName>
13
<EditTime>2007-11-07T18:55:41.9375+08:00</EditTime>
14
<states>1</states>
15
</Comment>
16
</iArray>
17
</CommentArray>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
