1 <?xml version="1.0" standalone="yes"?> 2 2 <NewDataSet> 3 3 <Table> 4 4 <Id>1</Id> 5 5 <Names>dawei</Names> 6 6 <Sex>男</Sex> 7 7 <Age>21</Age> 8 8 </Table> 9 9 <Table> 10 10 <Id>2</Id> 11 11 <Names>dain</Names> 12 12 <Sex>女</Sex> 13 13 <Age>20</Age> 14 14 </Table> 15 15 <Table> 16 16 <Id>3</Id> 17 17 <Names>xiyue/Names> 18 18 <Sex>女</Sex> 19 19 <Age>20</Age> 20 20 </Table> 21 21 <Table> 22 22 <Id>4</Id> 23 23 <Names>chenxin</Names> 24 24 <Sex>女</Sex> 25 25 <Age>16</Age> 26 26 </Table> 27 27 <Table> 28 28 <Id>5</Id> 29 29 <Names>kuai</Names> 30 30 <Sex>女</Sex> 31 31 <Age>20</Age> 32 32 </Table> 33 33 </NewDataSet>
下面是将数据库中的数据保存问XML格式
1 string con = "server=.; database = db_stu;Trusted_Connection = true"; 2 string cmdText = "select * from student"; 3 DataSet ds = new DataSet(); 4 using(SqlDataAdapter sda = new SqlDataAdapter(cmdText,con)) 5 { 6 sda.Fill(ds); 7 } 8 ds.WriteXml("a.xml"); 9 Console.WriteLine("Execute success"); 10 Console.ReadKey();
1 string con = "server=.; database = db_stu;Trusted_Connection = true"; 2 SqlConnection sc = new SqlConnection(con); 3 SqlDataAdapter da = new SqlDataAdapter(); 4 string cmdText = string.Format(@"insert into student(Names,Sex,Age) 5 values(@Names,@sex,@Age)"); 6 SqlCommand cmd = new SqlCommand(cmdText,sc); 7 try 8 { 9 10 sc.Open(); 11 DataSet dt = new DataSet(); 12 dt.ReadXml("a.xml"); 13 cmd.Parameters.Add("@Names",SqlDbType.VarChar,20,"Names"); 14 cmd.Parameters.Add("@sex",SqlDbType.VarChar,10,"Sex"); 15 cmd.Parameters.Add("@Age",SqlDbType.Int,10,"Age"); 16 da.InsertCommand = cmd; 17 da.Update(dt.Tables[0]); 18 Console.WriteLine("Execute success!"); 19 Console.ReadKey(); 20 }catch(Exception ex) 21 { 22 Console.WriteLine(ex.Message); 23 Console.ReadKey(); 24 } 25 finally 26 { 27 sc.Close(); 28 }