前台调用:
1
using System;
2
using Bmc;
3
4
public partial class _Default : System.Web.UI.Page
5
具体操作类:2
3
4
5
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
2
3
4
5
6
7
8
9
10
11
12
13
相关的实体类:
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
}
运行结果: 注意:最开始的XML说明缺少了encoding="utf-8"?
2
3
4
5
6
7
8
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
}
改进后的结果:
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