程序段中对语句作了说明。主要使用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
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
下面是通过上面程序生成的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
}
将数据更新到数据库的代码没有编写,自己弄吧^_^
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)