把数据库中的数据倒出一个XML文件,并把XML文件加载到DataGrid中
前台代码:
1
/// <summary>
2
/// 功能:把数据库中的数据倒出成为一个XML文件,把这个
3
///
4
/// 文件放在服务器中,并且现在又把这个XML文件加载
5
///
6
/// DataGrid中显示出来;
7
///
8
/// 时间:二00八年一月十日
9
///
10
/// 作者:曹代明
11
///
12
/// </summary>
13
using System;
14
using System.Data;
15
using System.Configuration;
16
using System.Collections;
17
using System.Web;
18
using System.Web.Security;
19
using System.Web.UI;
20
using System.Web.UI.WebControls;
21
using System.Web.UI.WebControls.WebParts;
22
using System.Web.UI.HtmlControls;
23
public partial class Jun_DataXML : System.Web.UI.Page
24
{
25
protected void Page_Load(object sender, EventArgs e)
26
{
27
}
28
29
/// <summary>
30
/// 从数据库在读出数据保存为XML数据
31
/// </summary>
32
/// <param name="sender"></param>
33
/// <param name="e"></param>
34
protected void Button1_Click(object sender, EventArgs e)
35
{
36
DataSet ds = BLL.DataXML.getData("select * from T_Unit");
37
ds.WriteXml(Server.MapPath("SimCard.xml"));
38
}
39
40
/// <summary>
41
/// 从XML数据加载到DataGrid
42
/// </summary>
43
/// <param name="sender"></param>
44
/// <param name="e"></param>
45
protected void Button2_Click(object sender, EventArgs e)
46
{
47
DataSet ds = new DataSet();
48
ds.ReadXml(Server.MapPath("SimCard.xml")); //保存在WEB虚拟站点下
49
this.DataGrid1.DataSource = ds;
50
this.DataGrid1.DataBind();
51
52
}
53
54
55
}
56

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

后台代码:
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataXML.aspx.cs" Inherits="Jun_DataXML" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="保存为XML数据" />
13
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="加载XML数据" />
14
<asp:DataGrid ID="DataGrid1" runat="server" Height="135px" Width="255px">
15
</asp:DataGrid>
16
17
</div>
18
</form>
19
</body>
20
</html>
21

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
