我有一个xml文件。文件包含产品名称,产品价格。
我正在更新所有产品的产品价格。但是我希望在更新每条记录后显示计数。
如果我在xml文件中有10个产品,那么它将显示
然后
最后它将显示10更新的10。
我正在显示进度条,但我想在更新时显示记录计数。
以下是C#代码。
这里我正在加载xml文件,然后它会读取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);
}
}
}
请告知。
答案 0 :(得分:1)
基本上,只需在代码中加一个计数器并在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;
然后您不需要不断重新创建进度条,并且您不需要在foreach
循环内为您处理的每个节点重新加载该XML文档 - 已完成在foreach
循环之前,在处理过程中不会发生变化。
答案 1 :(得分:0)
您只需在更新时保留一个计数器。完成每条记录后,添加到计数器并在任意位置显示该值。
更新: 将count变量取出循环。
e.g。
int count = 0;
foreach (XmlNode m_node_loopVariable in m_nodelist)
{
count += 1;