private void btnOut_Click(object sender, System.EventArgs e) {
DataSet ds = new DataSet();
this.sqlDataAdapter1.Fill(ds);
ds.WriteXml(Server.MapPath("\test.xml"),XmlWriteMode.IgnoreSchema);
ds.WriteXmlSchema(Server.MapPath("\test.xsd"));
}
private void btnInPut_Click(object sender, System.EventArgs e) {
DataSet ds = DSReadXml(Server.MapPath("\test.xml"));
this.DataGrid1.DataSource = ds.Tables[0];
this.DataGrid1.DataBind();
string sql = string.Empty;
foreach (DataRow dr in ds.Tables[0].Rows) {
sql += "insert into [webmenu] (";
for(int i = 0;i < ds.Tables[0].Columns.Count;i++) {
sql = sql + ds.Tables[0].Columns[i].ColumnName.ToString() + ",";
}
sql = sql.Substring(0,sql.Length - 1) + ") values (";
for(int x = 0;x < ds.Tables[0].Columns.Count;x++) {
sql = sql + "'" + dr[x].ToString().Replace("'","''") + "',";
}
sql = sql.Substring(0,sql.Length - 1) + ");";
}
SqlCommand sqlInsertCommand = new SqlCommand();
sqlInsertCommand.CommandText = sql;
sqlInsertCommand.Connection = this.sqlConnection1;
sqlConnection1.Open();
sqlInsertCommand.ExecuteNonQuery();
sqlConnection1.Close();
}
public static DataSet DSReadXml(string path) {
DataSet ds = new DataSet();
FileStream fs = null;
StreamReader reader = null;
try {
fs = new FileStream(path,FileMode.Open,FileAccess.Read);
reader = new StreamReader(fs);
ds.ReadXml(reader);
return ds;
}
finally {
fs.Close();
reader.Close();
}
}