簡體   English   中英

如何在 ASP.Net 中修改我的 xml 詳細信息?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM