NFe.Service.LerXML.Nfe C# (CSharp) Method

Nfe() public method

Faz a leitura do XML da nota fiscal eletrônica e disponibiliza os valores de algumas tag´s
public Nfe ( string arquivoXML ) : void
arquivoXML string Caminho e nome do arquivo XML da NFe a ser lido
return void
        public void Nfe(string arquivoXML)
        {
            ClearDados();

            XmlDocument doc = new XmlDocument();
            doc.Load(arquivoXML);

            XmlNodeList infNFeList = doc.GetElementsByTagName("infNFe");

            foreach (XmlNode infNFeNode in infNFeList)
            {
                XmlElement infNFeElemento = (XmlElement)infNFeNode;

                //Pegar a chave da NF-e
                if (infNFeElemento.HasAttributes)
                {
                    this.oDadosNfe.chavenfe = infNFeElemento.Attributes[TpcnResources.Id.ToString()].InnerText;
                    oDadosNfe.versao = infNFeElemento.Attributes[NFe.Components.TpcnResources.versao.ToString()].InnerText;
                }

                //Montar lista de tag´s da tag <ide>
                XmlNodeList ideList = infNFeElemento.GetElementsByTagName("ide");

                foreach (XmlNode ideNode in ideList)
                {
                    XmlElement ideElemento = (XmlElement)ideNode;

                    if (oDadosNfe.versao == "2.00")
                        oDadosNfe.dEmi = Convert.ToDateTime(Functions.LerTag(ideElemento, NFe.Components.TpcnResources.dEmi.ToString(), false));
                    else
                        oDadosNfe.dEmi = Convert.ToDateTime(Functions.LerTag(ideElemento, TpcnResources.dhEmi.ToString(), false));
                    oDadosNfe.cNF = Functions.LerTag(ideElemento, TpcnResources.cNF.ToString(), false);
                    oDadosNfe.nNF = Functions.LerTag(ideElemento, TpcnResources.nNF.ToString(), false);
                    oDadosNfe.tpEmis = Functions.LerTag(ideElemento, NFe.Components.TpcnResources.tpEmis.ToString(), false);
                    oDadosNfe.tpAmb = Functions.LerTag(ideElemento, TpcnResources.tpAmb.ToString(), false);
                    oDadosNfe.serie = Functions.LerTag(ideElemento, TpcnResources.serie.ToString(), false);
                    oDadosNfe.cUF = Functions.LerTag(ideElemento, NFe.Components.TpcnResources.cUF.ToString(), false);
                    oDadosNfe.mod = Functions.LerTag(ideElemento, TpcnResources.mod.ToString(), false);
                    oDadosNfe.cDV = Functions.LerTag(ideElemento, TpcnResources.cDV.ToString(), false);
                }

                //Montar lista de tag´s da tag <emit>
                XmlNodeList emitList = infNFeElemento.GetElementsByTagName("emit");

                foreach (XmlNode emitNode in emitList)
                {
                    XmlElement emitElemento = (XmlElement)emitNode;

                    this.oDadosNfe.CNPJ = Functions.LerTag(emitElemento, NFe.Components.TpcnResources.CNPJ.ToString(), false);
                }
            }

            //Tentar detectar a tag de lote, se tiver vai atualizar o atributo do lote que a nota faz parte
            XmlNodeList enviNFeList = doc.GetElementsByTagName("enviNFe");

            foreach (XmlNode enviNFeNode in enviNFeList)
            {
                XmlElement enviNFeElemento = (XmlElement)enviNFeNode;

                this.oDadosNfe.idLote = Functions.LerTag(enviNFeElemento, TpcnResources.idLote.ToString(), false);
            }
        }
        #endregion

Usage Example

Example #1
0
        /// <summary>
        /// Tratar exceção
        /// </summary>
        /// <param name="emp">Código da empresa</param>
        /// <param name="ex">Objeto com a exception</param>
        private void TrataException(int emp, Exception ex)
        {
            try
            {
                //Gravar o arquivo de erro de retorno para o ERP, caso ocorra
                switch (Empresas.Configuracoes[emp].IndSinc)
                {
                case true:
                    LerXML oLer = new LerXML();
                    oLer.Nfe(NomeArquivoXML);
                    if (oLer.oDadosNfe.indSinc)
                    {
                        TFunctions.GravarArqErroServico(NomeArquivoXML, Propriedade.ExtEnvio.EnvLot, Propriedade.ExtRetorno.ProRec_ERR, ex);
                    }
                    else
                    {
                        goto default;
                    }

                    break;

                default:
                    TFunctions.GravarArqErroServico(NomeArquivoXML, Propriedade.Extensao(Propriedade.TipoEnvio.EnvLot).EnvioXML, Propriedade.ExtRetorno.Rec_ERR, ex);
                    break;
                }
            }
            catch
            {
                //Se falhou algo na hora de gravar o retorno .ERR (de erro) para o ERP, infelizmente não posso fazer mais nada.
                //Wandrey 16/03/2010
            }
        }
All Usage Examples Of NFe.Service.LerXML::Nfe