我正在处理 xml 文件,我在这里找到了一个示例。我已经更改了连接字符串并创建了一个名为 MyProducts 的表,然后我在 App_Data 文件夹中手动找到了我的 Product.xml 文件。当我运行我的程序时得到这个执行
对象名称“产品”无效。
所以在调试模式下我注意到 myxml 变量是 null 我做错了什么
protected void Button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection connection;
SqlCommand command ;
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
XmlReader xmlFile ;
string sql = null;
int product_ID = 0;
string Product_Name = null;
double product_Price = 0;
connetionString = "Data Source=.\\sqlexpress;Initial Catalog=Northwind;Integrated Security=sspi";
connection = new SqlConnection(connetionString);
xmlFile = XmlReader.Create(Server.MapPath("~/App_Data/Product.xml"), new XmlReaderSettings());
ds.ReadXml(xmlFile);
int i = 0;
connection.Open();
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
product_Price = Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]);
sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";
command = new SqlCommand(sql, connection);
adpter.InsertCommand = command;
adpter.InsertCommand.ExecuteNonQuery();
}
connection.Close();
Label1.Text = "ok";
}