将 InnerHtml 值转换为十进制
本文关键字:十进制 转换 InnerHtml | 更新日期: 2023-09-27 18:31:21
我试图将InnerHtml值转换为十进制值。用户将在文本框中输入 XPath2
List<Decimal> productPriceList = new List<Decimal>();
var priceTags = document.DocumentNode.SelectNodes(textBox2.Text);
然后,我执行一个 foreach 循环来迭代找到的价格,并将它们添加到名为 productPriceList 的列表中
foreach (var prices in priceTags)
{
label9.Visible = true;
label9.Text += prices.InnerHtml + "'n";
productPriceList.Add(prices.InnerHtml);
label2.Visible = false;
label3.Visible = false;
}
然后我将值添加到数据库中:
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (var cmd = new SqlCommand(@"INSERT INTO OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, DateCaptured)
VALUES(@CompetitorID, @ProductCode, @ProductName, @DateCaptured)", con))
{
cmd.Parameters.Add("@CompetitorID", SqlDbType.Int);
cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar);
cmd.Parameters.Add("@ProductName", SqlDbType.VarChar);
cmd.Parameters.Add("@Price", SqlDbType.Decimal);
cmd.Parameters.Add("@DateCaptured", SqlDbType.DateTime);
for (int i = 0; i < competitorList.Count; i++)
{
cmd.Parameters["@CompetitorID"].Value = competitorList[i];
cmd.Parameters["@ProductCode"].Value = productCodeList[i];
cmd.Parameters["@ProductName"].Value = productNameList[i];
cmd.Parameters["@Price"].Value = productPriceList[i];
cmd.Parameters["@DateCaptured"].Value = dateCapturedList[i];
int rowsAffected = cmd.ExecuteNonQuery();
}
一切正常,但我似乎找不到将 InnerHtml 值正确转换为十进制类型的方法。有没有办法实现这一目标?
根据你来自内部html的值,你可以做这样的事情
using (var con = new SqlConnection(connectionString))
{
con.Open();
using (var cmd = new SqlCommand(@"INSERT INTO OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, DateCaptured)
VALUES(@CompetitorID, @ProductCode, @ProductName, @DateCaptured)", con))
{
cmd.Parameters.Add("@CompetitorID", SqlDbType.Int);
cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar);
cmd.Parameters.Add("@ProductName", SqlDbType.VarChar);
cmd.Parameters.Add("@Price", SqlDbType.Decimal);
cmd.Parameters.Add("@DateCaptured", SqlDbType.DateTime);
for (int i = 0; i < competitorList.Count; i++)
{
cmd.Parameters["@CompetitorID"].Value = competitorList[i];
cmd.Parameters["@ProductCode"].Value = productCodeList[i];
cmd.Parameters["@ProductName"].Value = productNameList[i];
cmd.Parameters["@Price"].Value = productPriceList[i];
cmd.Parameters["@DateCaptured"].Value = dateCapturedList[i];
int rowsAffected = cmd.ExecuteNonQuery(); // also the variable rowsAffected is not visible outside the scope of the for loop
}
}
}
更新:然后,您应该在将值添加到列表中之前转换这些值,如下所示:
foreach (var prices in priceTags)
{
label9.Visible = true;
label9.Text += prices.InnerHtml + "'n";
productPriceList.Add(Decimal.Parse(prices.InnerHtml, <cultureInfoHere>));
label2.Visible = false;
label3.Visible = false;
}
通过执行此操作,您不必更改列表类型。