C# 如何在ASP.Net中修改xml详细信息?
我不完全理解如何使用xml,但这就是我一直在研究的内容 这是我的添加产品代码。它插入到数据库和xml文件中。我花了4天的时间尝试让xml工作,现在它工作得很好。但现在的问题是,我不知道如何只编辑选定的id节点C# 如何在ASP.Net中修改xml详细信息?,c#,asp.net,C#,Asp.net,我不完全理解如何使用xml,但这就是我一直在研究的内容 这是我的添加产品代码。它插入到数据库和xml文件中。我花了4天的时间尝试让xml工作,现在它工作得很好。但现在的问题是,我不知道如何只编辑选定的id节点 if (FileUpload1.HasFile) { string str = FileUpload1.FileName; FileUpload1.PostedFile.Save
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>
3.
香蕉
12
Image/YellowBanana.jpg
香蕉比太阳还亮
4.
苹果
23
Image/Apple.jpg
非常红而且好吃
5.
芒果
17.9000
Image/AwesomeMango.jpg
多汁芒果
首先,插入时,需要获取新创建的Id。修改插入命令以从中获取Id。在INSERT
语句之后添加此项;选择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
(代码跟随)的函数
。。。
私有void appendToProducts(字符串xmlFilePath、int productId、字符串productName、字符串productImage、字符串productPrice、字符串productDescription){
XmlDocument xmlDoc=new System.Xml.XmlDocument();
//加载文件
load(xmlFilePath);
XmlElement productElement=xmlDoc.CreateElement(“产品”);//创建节点
//创建子节点
XmlElement ideElement=xmlDoc.CreateElement(“Id”);
xmlementnamelement=xmlDoc.CreateElement(“产品名称”);
xmlement priceElement=xmlDoc.CreateElement(“产品价格”);
xmlement imgElement=xmlDoc.CreateElement(“产品图像”);
XmlElement descElement=xmlDoc.CreateElement(“产品描述”);
idElement.InnerText=productId.ToString();
nameElement.InnerText=productName;
priceElement.InnerText=产品价格;
imgElement.InnerText=productImage;
descElement.InnerText=productDescription;
//将childs元素追加到产品节点
productElement.AppendChild(ideElement);
productElement.AppendChild(nameElement);
productElement.AppendChild(priceElement);
productElement.AppendChild(imgElement);
productElement.AppendChild(descElement);
//附加到根元素
xmlDoc.DocumentElement.AppendChild(productElement);
//拯救
保存(xmlFilePath)
}
...
请阅读Microsoft网站上关于Xml的更多信息:请更清楚地说明目标,我是否理解您希望修改逻辑,使其不
重新创建整个产品。Xml
文件,而只向其添加新创建的记录
?@JonathanLarouche感谢您的澄清。我需要的是修改现有的product.xml和现有节点。您想只处理insert
s还是同时update
s到product.xml文件?我目前正在编辑产品,所以只需更新到product.xml,我不知道您必须使用XmlDocument doc=new XmlDocument()
并使用doc.load(filePath)
加载物理文件。加载后,您将使用var-productnode=doc.CreateElement(“产品”)
创建一个新的productnode,用新数据填充它,然后使用doc.SelectSingleNode('//products').AppendChild(productnode)
附加到产品。完成后,将其保存到磁盘doc.save()
...
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)
}
...