程序段中对语句作了说明。主要使用XmlWriter类进行XML文件生成。
下面这是由关系数据库生成相应XML文件过程。由于XML只是中间件,所以忽略了Schema或者DTD。
1
private
void
Page_Load(
object
sender, System.EventArgs e)
2
{
3
// 在此处放置用户代码以初始化页面
4
//基本信息定义
5
String strTableName="systypes";
6
String strConnection="server=suntears;user id=sa;password=041210;database=webapplication1_db";
7
String strSql="select * from "+strTableName;
8
SqlConnection objConn=new SqlConnection(strConnection);
9
SqlDataAdapter objAdapter=new SqlDataAdapter(strSql,objConn);
10
DataSet objDSet=new DataSet();
11
objAdapter.Fill(objDSet,"temp");
12
XmlTextWriter objXmlWriter;
13
String strtemp1=Request.PhysicalApplicationPath;
14
String strpath=strtemp1+"qiming.xml";
15
//初始化XmlWriter.用此类写XML文件
16
objXmlWriter=new XmlTextWriter(strpath,null);
17
//创建开头的XML声明
18
objXmlWriter.WriteStartDocument();
19
//创建根元素xml1
20
objXmlWriter.WriteStartElement("xml1");
21
//表名为元素名,字段名为属性,表中的记录为属性的值
22
for(int i=0;i<objDSet.Tables["temp"].Rows.Count;i++)
23
{
24
objXmlWriter.WriteStartElement("menu");
25
for(int j=0;j<objDSet.Tables["temp"].Columns.Count;j++)
26
{
27
//写入属性
28
objXmlWriter.WriteAttributeString(objDSet.Tables["temp"].Columns[j].ColumnName,objDSet.Tables["temp"].Rows[i][j].ToString());
29
}
30
objXmlWriter.WriteEndElement();
31
}
32
objXmlWriter.WriteEndElement();
33
objXmlWriter.WriteEndDocument();
34
//用close方法关闭文件,否则文件将被锁定
35
objXmlWriter.Close();
36
// 在页面上显示结果
37
string strXmlResult;
38
StreamReader objSR = File.OpenText(strpath);
39
strXmlResult = objSR.ReadToEnd();
40
objSR.Close();
41
Response.Write("<pre>" + Server.HtmlEncode(strXmlResult) + "<pre>");
42
}

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

下面是通过上面程序生成的XML文件生成关系数据库的代码
1
private
void
Page_Load(
object
sender, System.EventArgs e)
2
{
3
// 在此处放置用户代码以初始化页面
4
//基本信息定义
5
int cx=1;
6
string strConn="server=suntears;user id=sa;password=;database=xml_example";
7
string strappstr=Request.PhysicalApplicationPath;
8
string strpath=strappstr+"qiming.xml";
9
string strtablename="xml1";
10
string strCreatetable="create table "+strtablename+"(";
11
XmlTextReader objXmlReader=new XmlTextReader(strpath);
12
SqlConnection objConn=new SqlConnection(strConn);
13
SqlCommand objCommand;
14
SqlDataAdapter objAdapter;
15
DataSet objDSet=new DataSet();
16
DataRow objrow;
17
XmlNodeType objNodeType;
18
//采用XmlReader类进行XML文件访问操作
19
//XmlReader采用拉(pull)模式。因此通过Read()方法迭代各个节点
20
while(objXmlReader.Read())
21
{
22
objNodeType=objXmlReader.NodeType;
23
//根据节点类型进行不同操作
24
switch(objNodeType)
25
{
26
//XML文件开头的声明
27
case XmlNodeType.XmlDeclaration:
28
Response.Write("11111"+objXmlReader.Name+"<br>");
29
break;
30
//正常节点类型
31
case XmlNodeType.Element:
32
//根据属性个数判断是否是根元素
33
if(objXmlReader.AttributeCount>0)
34
{
35
//cx作为标志位。第一次访问非根元素节点时根据元素结构生成数据库
36
if(cx==1)
37
{
38
while(objXmlReader.MoveToNextAttribute())
39
{
40
strCreatetable=strCreatetable+objXmlReader.Name+" varchar(50), ";
41
}
42
strCreatetable=strCreatetable+")";
43
objCommand=new SqlCommand(strCreatetable,objConn);
44
objConn.Open();
45
objCommand.ExecuteNonQuery();
46
objConn.Close();
47
objAdapter=new SqlDataAdapter("select * from "+strtablename,objConn);
48
objAdapter.Fill(objDSet,strtablename);
49
objXmlReader.MoveToFirstAttribute();
50
//更改标志为cx数值
51
cx=0;
52
}
53
//将XML文件中数据转存到DataSet对象中
54
objrow=objDSet.Tables[strtablename].NewRow();
55
objXmlReader.MoveToFirstAttribute();
56
for(int j=0;j<objXmlReader.AttributeCount;j++)
57
{
58
objrow[j]=objXmlReader.Value.ToString();
59
objXmlReader.MoveToNextAttribute();
60
}
61
objDSet.Tables[strtablename].Rows.Add(objrow);
62
}
63
break;
64
}
65
}
66
//显示转存后的数据表
67
DataGrid1.DataSource=objDSet.Tables[strtablename];
68
DataGrid1.DataBind();
69
}
将数据更新到数据库的代码没有编写,自己弄吧^_^

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
