The statements are explained in the program section. The XmlWriter class is mainly used for XML file generation.
The following is the process of generating the corresponding XML file from the relational database. Since XML is just middleware, Schema or DTD is ignored.
1 private void Page_Load(object sender, System.EventArgs e)
2 {
3 //Put user code here to initialize the page
4 //Basic information definition
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 //Initialize XmlWriter. Use this class to write XML files
16 objXmlWriter=new XmlTextWriter(strpath,null);
17 //Create the XML declaration at the beginning
18 objXmlWriter.WriteStartDocument();
19 //Create root element xml1
20 objXmlWriter.WriteStartElement("xml1");
21 //The table name is the element name, the field name is the attribute, and the records in the table are the values of the attributes.
22 for(int i=0;i<objDSet.Tables["temp"].Rows.Count;i++)
twenty three {
24 objXmlWriter.WriteStartElement("menu");
25 for(int j=0;j<objDSet.Tables["temp"].Columns.Count;j++)
26 {
27 //Write attributes
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 //Use the close method to close the file, otherwise the file will be locked
35 objXmlWriter.Close();
36 // Display the results on the page
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 }
The following is the code to generate a relational database through the XML file generated by the above program
1 private void Page_Load(object sender, System.EventArgs e)
2 {
3 //Put user code here to initialize the page
4 //Basic information definition
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 //Use the XmlReader class for XML file access operations
19 //XmlReader adopts pull mode. Therefore, each node is iterated through the Read() method
20 while(objXmlReader.Read())
twenty one {
22 objNodeType=objXmlReader.NodeType;
23 //Perform different operations based on node type
24 switch(objNodeType)
25 {
26 //Declaration at the beginning of the XML file
27 case XmlNodeType.XmlDeclaration:
28 Response.Write("11111"+objXmlReader.Name+"<br>");
29 break;
30 //Normal node type
31 case XmlNodeType.Element:
32 //Determine whether it is a root element based on the number of attributes
33 if(objXmlReader.AttributeCount>0)
34 {
35 //cx as flag bit. Generate a database based on the element structure when accessing a non-root element node for the first time
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 //Change the flag to cx value
51cx=0;
52 }
53 //Dump the data in the XML file into the DataSet object
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 //Display the transferred data table
67 DataGrid1.DataSource=objDSet.Tables[strtablename];
68 DataGrid1.DataBind();
69 }The code to update the data to the database has not been written. Do it yourself^_^