Show xml nfe tag value in datagridview columns

Asked

Viewed 130 times

0

I have a little problem because I am reading the tags of an xml of Nfe, but I am not able to show in the columns of datagridview, my code is reading the correct tags, but do not notice anything in datagridview.

follows the xml part I’m reading

-<det nItem="1">


-<prod>

<cProd>PA3003</cProd>

<cEAN/>

<xProd>OLEO ESSENCIAL DE CITRONELA</xProd>

<NCM>33012911</NCM>

<CEST>2000600</CEST>

<EXTIPI>00</EXTIPI>

<CFOP>5101</CFOP>

<uCom>KG</uCom>

<qCom>25.0000</qCom>

<vUnCom>123.900000000</vUnCom>

<vProd>3097.50</vProd>

<cEANTrib/>

<uTrib>KG</uTrib>

<qTrib>25.0000</qTrib>

<vUnTrib>123.900000000</vUnTrib>

<indTot>1</indTot>

<xPed>002497/1</xPed>

</prod>

follows my code

private void btn_xml_Click(object sender, EventArgs e)
{
    string FileName = @"C:\Xml_Entrada\2053- CITROLEO.xml";
    List<string> ListaItens = new List<string>();
    XmlDocument doc = new XmlDocument();
    doc.Load(FileName);
    var proditens = doc.GetElementsByTagName("prod");
    foreach (XmlElement nodo in proditens)
    {
            ListaItens.Add(nodo.GetElementsByTagName("cProd")[0].InnerText.Trim());
            ListaItens.Add(nodo.GetElementsByTagName("xProd")[0].InnerText.Trim());
            ListaItens.Add(nodo.GetElementsByTagName("qCom")[0].InnerText.Trim());
    }

    dgw_Xml.DataSource = ListaItens;

}

Follow the result inserir a descrição da imagem aqui

class ClasseItensXml
{
    string CodigoProduto;
    string NomeProduto;
    string QtdProduto;

    public string CodigoP
    {
        get { return CodigoProduto; }
        set { CodigoProduto = value; }
    }

    public string NomeP
    {
        get { return NomeProduto; }
        set { NomeProduto = value; }
    }
    public string QtdP
    {
        get { return QtdProduto; }
        set { QtdProduto = value; }
    }
}
  • you’re putting in the ListaItens and putting as source the proditens...

  • So Rovann thanks for the attention, but when I put the Item Listings, I only see a column in the datagridview with the size of the xml tags as shown below

  • 1

    You have to edit your question, not publish as an answer. Datagridview expects to receive rows and columns to display the data, you have to provide them in this format. Add the code of the class you want to display (I imagine it is ListaItens) that we can demonstrate how to do.

  • If you can show me how to do thank you.

1 answer

0


First let’s change your class, just for aesthetic reasons:

public class ClasseItensXml
{
    public string CodigoProduto {get;set;}
    public string NomeProduto {get;set;}
    public string QuantidadeComercializada {get;set;}
}

After the code, here you just change the list to the type of the items, and then use it as source:

private void btn_xml_Click(object sender, EventArgs e)
{
    string FileName = @"C:\Xml_Entrada\2053- CITROLEO.xml";
    List<ClasseItensXml> ListaItens = new List<ClasseItensXml>(); //A lista é do tipo 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 = nodo.GetElementsByTagName("qCom")[0].InnerText.Trim()
                 });

            //Repare que cada "nodo" é um item, portanto só adiciona um ClasseItensXml na lista.
    }

    dgw_Xml.DataSource = ListaItens; //por fim, usa a lista de source

}

ps. I would use decimal in quantity, but to simplify the answer I didn’t change it.

  • Thank you very much Rovann gave it right here vlw.

  • Rovann can ask me a question, I’m trying to turn the Quantitymercialized string to Decimal, only that the result in the column on the datagrid, is wrong, for example. I have 360.000 when I turn to decimal column the number is 3.600.000,00, how can I solve it. Can you help me.

  • where is the code you are using for conversion ?!

  • see this code: https://dotnetfiddle.net/DN2P8m

  • So Rovann, I need to create a function to do the conversion before, because what I’m doing, was just changing the class from string to decimal, thinking that would solve the problem. Taking public string Quantitymercialized {get;set;} and switching to public decimal Quantitymercialized {get;set;}.

  • then you will have to convert the string that comes from xml...

  • Rovann, this example formula that you sent me, this right after I convert my xml returns as it is in the example that you sent me, the problem and that I am making a division using this value. Ex the return of the xml in decimal this showing me this way 3600000 only that this value and certain quantity would appear only 360 that and the quantity of the item in xml. The way it appears 3600000 when I divide by 2 the result and 1.800.000,00, it should be 180.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.