1
I’m trying to create a C# script for a Plugin in Notepad++, where the goal is to add all the values inside the tags <ValorDesconto>
, <ValorCancelamento>
and <ValorTotalLiquido>
of the XML file that is open and display the result.
For knowledge only, the Plugin inside Notepad++ is called Automation Scripts, follow:
The values of tags ALWAYS are formatted with only two decimal places, so for a product value of R$21,347.10 (example), in XML will appear as 21347,10.
This is an example of XML:
<Produtos>
<Produto>
<Descricao>ESMALTE</Descricao>
<Quantidade>2,00</Quantidade>
<Unidade>UN</Unidade>
<ValorDesconto>2,00</ValorDesconto>
<ValorAcrescimo>0,00</ValorAcrescimo>
<ValorCancelamento>3,00</ValorCancelamento>
<ValorTotalLiquido>23,80</ValorTotalLiquido>
</Produto>
<Produto>
<Descricao>PINCEL</Descricao>
<Quantidade>5,00</Quantidade>
<Unidade>UN</Unidade>
<ValorDesconto>3,00</ValorDesconto>
<ValorAcrescimo>0,00</ValorAcrescimo>
<ValorCancelamento>8,00</ValorCancelamento>
<ValorTotalLiquido>32,10</ValorTotalLiquido>
</Produto>
</Produtos>
Inside the XML file the tag <Produto>
(inside <Produtos>
) repeats several times, one for each item, the same goes for the tag <Produtos>
that repeats several times as well. In this example, the script would have to add the tags I mentioned and return the value of R$71.90.
Below is my code in C# that was made by a member here in the community but that is always returning the value "R$0" as you can see in the image below.
using System;
using System.Xml;
using System.Windows.Forms;
using NppScripts;
public class Script : NppScript
{
public override void Run()
{
string path = Npp.Editor.GetCurrentFilePath();
MessageBox.Show(path);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
string xpath = "Produtos/Produto";
XmlNodeList nodes = xmlDoc.SelectNodes(xpath);
decimal values = 0;
foreach(XmlNode childrenNode in nodes)
{
decimal valorDesconto = Decimal.Parse(childrenNode.SelectSingleNode(".//ValorDesconto").InnerText.Replace(',','.'));
decimal valorCancelamento = Decimal.Parse(childrenNode.SelectSingleNode(".//ValorCancelamento").InnerText.Replace(',','.'));
decimal valorLiquido = Decimal.Parse(childrenNode.SelectSingleNode(".//ValorLiquido").InnerText.Replace(',','.'));
values += valorDesconto + valorCancelamento + valorLiquido;
}
string result = "R$" + values.ToString().Replace('.',',');
MessageBox.Show(result);
}
}
Where is the error in the code?
I replaced my code with yours, but here it didn’t work and returned error. What can it be? I put in Imgur: https://imgur.com/a/iY1TSZ6
– Roger Windberg