Importing an XML and using columns to make calculations

0

I need a help, I'm importing NFE xml, and I'm using the QTDA field to make a calculation, except that the quantity field in xml is composed in this way, 360.0000 and when I import that field, it is 3600000 because I am turning to decimal in the code, and also use this QTDA column to make a calculation by Ex. QTDA / QTDA column. EMB. in case it would be 3600000/2 = 1,800,000.00 but this is wrong this result the correct result should be 180 and the QTDA field should return from XML 360 and not 3600000 as it is returning. How can I resolve this?

Follow the XML

<prod>
  <cProd>CB60007/0180/6P32</cProd>
  <cEAN/>
  <xProd>CRODAPEARL AF-LQ-(BR) 006416</xProd>
  <NCM>38249929</NCM>
  <CEST>2806300</CEST>
  <CFOP>5101</CFOP>
  <uCom>KG</uCom>
  <qCom>360.0000</qCom>
  <vUnCom>8.5600000000</vUnCom>
  <vProd>3081.60</vProd>
  <cEANTrib/>
  <uTrib>KG</uTrib>
  <qTrib>360.0000</qTrib>
  <vUnTrib>8.5600000000</vUnTrib>
  <indTot>1</indTot>
  <xPed>002459</xPed>
  <nFCI>D89A9C05-4493-4740-937A-5DA6B9E675A8</nFCI>
</prod>

Code

private void btn_xml_Click(object sender, EventArgs e) {

  string FileName = @ "C:\Xml_Entrada\" + txt_chave.Text + ".xml ";
  List < ClasseItensXml > ListaItens = new List < ClasseItensXml > ();
  XmlDocument doc = new XmlDocument();
  doc.Load(FileName);
  var proditens = doc.GetElementsByTagName("prod");

  foreach(XmlElement nodo in proditens) {
    ListaItens.Add(
      new ClasseItensXml() {
        CodigoProduto = nodo.GetElementsByTagName("cProd")[0].InnerText.Trim(),
          NomeProduto = nodo.GetElementsByTagName("xProd")[0].InnerText.Trim(),
          QuantidadeComercializada = Convert.ToDecimal(nodo.GetElementsByTagName("qCom")[0].InnerText.Trim())
      });
  }

  dgw_Xml.DataSource = ListaItens;
}
private void dgw_Xml_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
  foreach(DataGridViewRow row in dgw_Xml.Rows) {
    if (e.ColumnIndex == dgw_Xml.Columns["QtdaEmbalagem"].Index) {
      dgw_Xml.CurrentRow.Cells["ConferenciaPB"].Value = ConferirPB();
      dgw_Xml.CurrentRow.Cells["ConferenciaPL"].Value = ConferirPL();
    }
  }
}
private decimal ConferirPB() {
  decimal pesoBruto = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["PesoBruto"].Value);
  decimal qtdaEmbalagem = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["QtdaEmbalagem"].Value);
  return pesoBruto / qtdaEmbalagem;
}
private decimal ConferirPL() {
  decimal pesoLiquido = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["QuantidadeComercializada"].Value);
  decimal qtdaEmbalagem = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["QtdaEmbalagem"].Value);
  return pesoLiquido / qtdaEmbalagem;
}
class ClasseItensXml {
  public string CodigoProduto { get; set;}      
  public string NomeProduto { get; set;}
  public decimal QuantidadeComercializada { get; set;}
  public decimal PesoBruto { get; set;}
  public decimal ConferenciaPB { get; set;}
  public decimal ConferenciaPL { get; set;}
  public decimal QtdaEmbalagem { get; set;}
  public string LoteFornecedor { get; set;}
  public DateTime DataFabricacao{ get; set;}
  public DateTime DataValidade { get; set;}
}

Follow the result

    
asked by anonymous 19.07.2018 / 17:49

1 answer

0

Set CultureInfo works correctly:

decimal qtdaEmbalagem = Convert.ToDecimal(dgw_Xml.CurrentRow.Cells["QtdaEmbalagem"].Value),
                        System.Globalization.CultureInfo.InvariantCulture);

See an example here: link

    
19.07.2018 / 18:42