C# 更新每个记录后显示计数
我有一个xml文件。文件包含产品名称、产品价格 我正在更新所有产品的产品价格。但我希望在每次更新记录后显示计数 比如,如果我有10个xml文件中的产品,那么它就会显示出来C# 更新每个记录后显示计数,c#,.net,xml,winforms,C#,.net,Xml,Winforms,我有一个xml文件。文件包含产品名称、产品价格 我正在更新所有产品的产品价格。但我希望在每次更新记录后显示计数 比如,如果我有10个xml文件中的产品,那么它就会显示出来 第1页,共10页 然后 第2页,共10页 最后,它将显示10个更新的10个 我正在显示进度条,但我想在更新时也显示记录计数 下面是C代码 在这里,我加载xml文件,然后它将读取productcode节点,并使用我们在文本框中输入的内容更新价格 更新每个产品价格后,我希望显示记录计数 using System; usin
- 第1页,共10页
- 第2页,共10页
productcode
节点,并使用我们在文本框中输入的内容更新价格
更新每个产品价格后,我希望显示记录计数
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using APIReaderLib;
using APIReaderLib.DataObjects;
namespace VAPIReader
{
public partial class UpdateProducts : Form
{
public UpdateProducts()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
XmlDocument m_xmld = null;
XmlNodeList m_nodelist = null;
XmlNode m_node = null;
XMLPostManager manager = new XMLPostManager();
m_xmld = new XmlDocument();
m_xmld.Load("C:\\Users\\pooja.b.EDREAMZ\\Desktop\\s&p\\Final1.xml");
m_nodelist = m_xmld.SelectNodes("/xmldata/Products");
foreach (XmlNode m_node_loopVariable in m_nodelist)
{
m_node = m_node_loopVariable;
string Productcode = m_node.ChildNodes.Item(0).InnerText;
string productprice = m_node.ChildNodes.Item(1).InnerText;
Console.Write(" Product Code: " + Productcode + "Product Price:" + productprice);
decimal strprice =Convert.ToDecimal( productprice);
decimal strtextprice = Convert.ToDecimal(textBox1.Text);
decimal test = strprice + (strprice * strtextprice/100);
string updatedprice = test.ToString();
UpdateProduct(updatedprice, Productcode);
XmlDocument readDoc = new XmlDocument();
readDoc.Load("C:\\Users\\pooja.b.EDREAMZ\\Desktop\\s&p\\Final1.xml");
int count = readDoc.SelectNodes("/xmldata/Products").Count;
progressBar1.Minimum = 1;
// Set Maximum to the total number of Users created.
progressBar1.Maximum = count;
// Set the initial value of the ProgressBar.
progressBar1.Step = 1;
progressBar1.PerformStep();
// Updates the label to show that a file was read.
label2.Text = Convert.ToString(progressBar1.Value) + "updated of " + count;
}
}
catch (Exception ex)
{
MessageBox.Show("Error updating product " + ex.Message);
}
this.Close();
System.Windows.Forms.MessageBox.Show("Complete!");
}
private void UpdateProduct(string price, string Productcode)
{
xmldata1 data = new xmldata1();
data.Items = new xmldataProducts[1];
data.Items[0] = new xmldataProducts();
data.Items[0].ProductPrice = price;
data.Items[0].ProductCode = Productcode;
string productXML = Utils.GetProductXML(data);
string APIURL = Utils.GetAPIPostURL(ImportMode.Update);
XMLPostManager manager = new XMLPostManager();
string response = manager.SendXMLToURL(APIURL, productXML);
}
}
}
请告知。您只需在更新时保留一个计数器即可。完成每个记录后,将其添加到计数器中,并根据需要显示该值 更新: 将count变量从循环中取出 e、 g
基本上,只需在代码中设置一个计数器,并在
foreach
循环中递增它,然后输出:
m_nodelist = m_xmld.SelectNodes("/xmldata/Products");
int totalCount = m_nodelist.Count; // insert this - total count
int handled = 0; // insert this - currently handled count
然后在处理节点后,更新进度条时,包括以下内容:
progressBar1.PerformStep();
handled++; // increment the handled count
// output message of "updated x of y nodes"
Console.WriteLine("Updated {0} of {1} nodes", handled, totalCount);
另外:您应该只在foreach
循环之外设置一次进度条!比如:
progressBar1.Minimum = 1;
// Set Maximum to the total number of Users created.
progressBar1.Maximum = totalCount; // you've already determined that total count before!
// Set the initial value of the ProgressBar.
progressBar1.Step = 1;
这样你就不需要不断地重新创建progressbar,对于您处理的每个节点,您不需要在
foreach
循环中重新加载该XML文档-这在foreach
循环之前已完成一次,并且在处理过程中不会更改。在更新XML记录时增加计数器值。什么类型的应用程序?Winforms?安慰WPF?ASP.NET??你试过什么??向我们展示您更新产品价格的代码…请查看以上代码。让我知道你的评论。是的,但它不工作,例如,如果我有10条记录,然后。完成foreach循环后,它会直接向我显示10条更新的10条记录。它在一个线程中执行。请告诉我,我已经按照你说的做了改变,但它仍然直接向我显示了10个更新的10个。在一个线程中执行的for循环,这就是它在更新10条记录后显示的原因。更新一条记录后,将继续更新第二条记录,然后更新第三条记录,依此类推。然后,当它将完成10条记录,然后它会显示我的计数。请参阅更新的代码。您得到了所有产品的计数,当您在循环每个产品时,每次只需将计数更新1。
progressBar1.Minimum = 1;
// Set Maximum to the total number of Users created.
progressBar1.Maximum = totalCount; // you've already determined that total count before!
// Set the initial value of the ProgressBar.
progressBar1.Step = 1;