[英]How do i modify my xml details in ASP.Net?
我不完全了解如何 xml 但这是我一直在做的。
这是我的添加产品代码。 它插入数据库和 xml 文件。 我花了 4 天时间试图让 xml 工作,现在它工作得很好。 但现在的问题是我不知道如何只编辑选定的 id 节点
if (FileUpload1.HasFile)
{
string str = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Image/" + str));
string Image = "Image/" + str.ToString();
con.Open();
string query = "INSERT INTO Product (product_Name, product_image, product_Price,product_Description) VALUES (@product_Name, @product_image, @product_Price, @product_Description)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@product_Name", product_name.Text);
cmd.Parameters.AddWithValue("@product_image", Image);
cmd.Parameters.AddWithValue("@product_Price", product_price.Text);
cmd.Parameters.AddWithValue("@product_Description", product_description.Text.Trim());
cmd.ExecuteNonQuery();
string strSQL = "SELECT * FROM Product";
SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet("productdetails");
dt.Fill(ds, "product");
ds.WriteXml(Server.MapPath(@".\xml\products.xml"));
con.Close();
Response.Redirect("AdminMenu.aspx");
}
else
{
Label5.Text = "Please Upload your Image";
Label5.ForeC
这是 xml 的编辑产品代码。 数据库站点没有问题。
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Product WHERE Id=" + contact_id))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string id = row["Id"].ToString();
string Name = row["Product_Name"].ToString();
string Price = row["Product_Price"].ToString();
string Description = row["Product_Description"].ToString();
this.HiddenField1.Value = id;
this.TextBoxName.Text = Name;
this.TextBoxPrice.Text = Price;
this.TextBoxDescription.Text = Description;
}
}
}
}
}
我在这里想要实现的是更新product.xml中的现有细节。
XML 内容
<?xml version="1.0" standalone="yes"?>
<productdetails>
<product>
<Id>3</Id>
<Product_Name>Banana</Product_Name>
<Product_Price>12.0000</Product_Price>
<Product_image>Image/YellowBanana.jpg</Product_image>
<Product_Description>Banana brighter than the sun</Product_Description>
</product>
<product>
<Id>4</Id>
<Product_Name>Apple</Product_Name>
<Product_Price>23.0000</Product_Price>
<Product_image>Image/Apple.jpg</Product_image>
<Product_Description>Very Red and Delicious</Product_Description>
</product>
<product>
<Id>5</Id>
<Product_Name>Mango</Product_Name>
<Product_Price>17.9000</Product_Price>
<Product_image>Image/AwesomeMango.jpg</Product_image>
<Product_Description>Juicy Fruit Mango</Product_Description>
</product>
</productdetails>
首先,在插入的时候,需要获取新创建的Id。 修改您的插入命令以从中获取 Id。 在INSERT
语句之后添加它;SELECT SCOPE_IDENTITY()
并使用ExecuteScalar()
检索它。
...
string query = "INSERT INTO Product (product_Name, product_image, product_Price,product_Description) VALUES (@product_Name, @product_image, @product_Price, @product_Description); SELECT SCOPE_IDENTITY();";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@product_Name", product_name.Text);
cmd.Parameters.AddWithValue("@product_image", Image);
cmd.Parameters.AddWithValue("@product_Price", product_price.Text);
cmd.Parameters.AddWithValue("@product_Description", product_description.Text.Trim());
int newId = (int)cmd.ExecuteScalar();
//Call function to add product info to products.xml file
appendToProducts(Server.MapPath(@".\xml\products.xml"),newId, product_name.Text, Image, product_price.Text, product_description.Text.Trim());
...
然后调用名为appendToProducts
的 function(代码如下)
...
private void appendToProducts(string xmlFilePath, int productId, string productName, string productImage, string productPrice, string productDescription) {
XmlDocument xmlDoc = new System.Xml.XmlDocument();
//LOAD FILE
xmlDoc.load(xmlFilePath);
XmlElement productElement = xmlDoc.CreateElement("product"); //CREATE <product> node
//Create child nodes
XmlElement idElement = xmlDoc.CreateElement("Id");
XmlElement nameElement = xmlDoc.CreateElement("Product_Name");
XmlElement priceElement = xmlDoc.CreateElement("Product_Price");
XmlElement imgElement = xmlDoc.CreateElement("Product_image");
XmlElement descElement= xmlDoc.CreateElement("Product_Description");
idElement.InnerText = productId.ToString();
nameElement.InnerText = productName;
priceElement.InnerText = productPrice;
imgElement.InnerText = productImage;
descElement.InnerText = productDescription;
//Append childs element to product node
productElement.AppendChild(idElement);
productElement.AppendChild(nameElement);
productElement.AppendChild(priceElement);
productElement.AppendChild(imgElement);
productElement.AppendChild(descElement);
//Append <product> to <productdetails> root element
xmlDoc.DocumentElement.AppendChild(productElement);
//Save
xmlDoc.Save(xmlFilePath)
}
...
在 Microsoft 网站上阅读有关 Xml 的更多信息: https://docs.microsoft.com/en-us/dotnet/api/system.xml?view=network-48.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.