示例#1
0
        private void btn_Validar_Click(object sender, EventArgs e)
        {
            LimparEPosicionarTC();

            try
            {
                if (this.edtFilename.Text == "" || !File.Exists(this.edtFilename.Text))
                {
                    this.textBox_resultado.Text = "Arquivo não encontrado.";
                    return;
                }

                //Copiar o arquivo XML para temporários para assinar e depois vou validar o que está nos temporários
                FileInfo oArquivo = new FileInfo(this.edtFilename.Text);
                string   cArquivo = System.IO.Path.GetTempPath() + oArquivo.Name;

                oArqDestino = new FileInfo(cArquivo);

                oArquivo.CopyTo(cArquivo, true);

                //Remover atributo de somente leitura que pode gerar problemas no acesso do arquivo
                NFe.Service.TFunctions.RemoveSomenteLeitura(cArquivo);

                NFe.Service.TaskValidar val = new Service.TaskValidar();
                val.NomeArquivoXML = oArqDestino.FullName;
                val.Execute();
                int codUF = Empresas.Configuracoes[Emp].UnidadeFederativaCodigo;
                //Detectar o tipo do arquivo
                PadroesNFSe padraoNFSe = Functions.PadraoNFSe(Empresas.Configuracoes[Emp].UnidadeFederativaCodigo);
                if (padraoNFSe == PadroesNFSe.BETHA)
                {
                    string versao = Functions.GetAttributeXML("LoteRps", "versao", cArquivo);
                    if (versao.Equals("2.02"))
                    {
                        codUF = 202;
                    }
                }
                Validate.ValidarXML validarXML = new Validate.ValidarXML(cArquivo, codUF, false);

                string resultValidacao = "";

                XmlDocument conteudoXML = new XmlDocument();
                try
                {
                    conteudoXML.Load(cArquivo);
                }
                catch
                {
                    conteudoXML.LoadXml(File.ReadAllText(cArquivo, System.Text.Encoding.UTF8));
                }

                if (conteudoXML.DocumentElement.Name.Equals("CTe") ||
                    conteudoXML.DocumentElement.Name.Equals("MDFe"))
                {
                    XmlDocument infModal = new XmlDocument();
                    XmlDocument modal    = new XmlDocument();

                    if (conteudoXML.GetElementsByTagName("infModal")[0] != null)
                    {
                        foreach (XmlElement item in conteudoXML.GetElementsByTagName("infModal"))
                        {
                            infModal.LoadXml(item.OuterXml);
                            modal.LoadXml(item.InnerXml);
                        }

                        Validate.ValidarXML validarModal = new Validate.ValidarXML(infModal, Empresas.Configuracoes[Emp].UnidadeFederativaCodigo, false);
                        resultValidacao += validarModal.ValidarArqXML(modal, cArquivo);
                    }
                }

                this.textBox_resultado.Text = validarXML.TipoArqXml.cRetornoTipoArq;

                if (validarXML.TipoArqXml.nRetornoTipoArq >= 1 && validarXML.TipoArqXml.nRetornoTipoArq <= SchemaXML.MaxID)
                {
                    this.edtTipoarquivo.Text = validarXML.TipoArqXml.cRetornoTipoArq;

                    //Assinar o arquivo XML copiado para a pasta TEMP
                    bool lValidar         = false;
                    AssinaturaDigital oAD = new AssinaturaDigital();
                    try
                    {
                        validarXML.EncryptAssinatura(cArquivo);
                        oAD.Assinar(cArquivo,
                                    Emp,
                                    Empresas.Configuracoes[Emp].UnidadeFederativaCodigo,
                                    (conteudoXML.DocumentElement.Name.Equals("Reinf") || conteudoXML.DocumentElement.Name.Equals("eSocial") ? AlgorithmType.Sha256 : AlgorithmType.Sha1));

                        lValidar = true;
                    }
                    catch (Exception ex)
                    {
                        lValidar = false;
                        this.textBox_resultado.Text = "Ocorreu um erro ao tentar assinar o XML: \r\n\r\n" +
                                                      validarXML.TipoArqXml.cRetornoTipoArq + "\r\n" + ex.Message;
                    }

                    if (lValidar == true)
                    {
                        //Validar o arquivo
                        if (validarXML.TipoArqXml.nRetornoTipoArq >= 1 && validarXML.TipoArqXml.nRetornoTipoArq <= SchemaXML.MaxID)
                        {
                            ///danasa: 12/2013
                            validarXML.ValidarArqXML(cArquivo);
                            if (string.IsNullOrEmpty(validarXML.TipoArqXml.cArquivoSchema))
                            {
                                this.textBox_resultado.Text = "XML não possui schema de validação, sendo assim não é possível validar XML";
                            }
                            else if (validarXML.Retorno == 0 && string.IsNullOrEmpty(resultValidacao))
                            {
                                this.textBox_resultado.Text = "Arquivo validado com sucesso!";
                            }
                            else if (!string.IsNullOrEmpty(resultValidacao))
                            {
                                this.textBox_resultado.Text = resultValidacao;
                            }
                            else
                            {
                                this.textBox_resultado.Text = "XML INCONSISTENTE!\r\n\r\n" + validarXML.RetornoString;
                            }
                        }
                        else
                        {
                            //this.textBox_tipoarq.Text = validarXML.TipoArqXml.cRetornoTipoArq;
                            this.textBox_resultado.Text = "XML INCONSISTENTE!\r\n\r\n" + validarXML.TipoArqXml.cRetornoTipoArq;
                        }
                    }
                }

                try
                {
                    if (wb == null)
                    {
                        wb                    = new WebBrowser();
                        wb.Parent             = this.metroTabPage2;
                        wb.Dock               = DockStyle.Fill;
                        wb.DocumentCompleted += webBrowser1_DocumentCompleted;
                    }
                    wb.Visible = true;
                    wb.Navigate(cArquivo);
                }
                catch
                {
                    webBrowser1_DocumentCompleted(null, null);
                }
            }
            catch (Exception ex)
            {
                this.textBox_resultado.Text = ex.Message + "\r\n" + ex.StackTrace;

                if (wb != null)
                {
                    wb.Visible = false;
                }
            }
        }
示例#2
0
        private void btn_Validar_Click(object sender, EventArgs e)
        {
            LimparEPosicionarTC();

            try
            {
                if (this.edtFilename.Text == "" || !File.Exists(this.edtFilename.Text))
                {
                    this.textBox_resultado.Text = "Arquivo não encontrado.";
                    return;
                }

                //Copiar o arquivo XML para temporários para assinar e depois vou validar o que está nos temporários
                FileInfo oArquivo = new FileInfo(this.edtFilename.Text);
                string   cArquivo = System.IO.Path.GetTempPath() + oArquivo.Name;

                oArqDestino = new FileInfo(cArquivo);

                oArquivo.CopyTo(cArquivo, true);

                //Remover atributo de somente leitura que pode gerar problemas no acesso do arquivo
                NFe.Service.TFunctions.RemoveSomenteLeitura(cArquivo);

                NFe.Service.TaskValidar val = new Service.TaskValidar();
                val.NomeArquivoXML = oArqDestino.FullName;
                val.Execute();

                //Detectar o tipo do arquivo
                NFe.Validate.ValidarXML validarXML = new NFe.Validate.ValidarXML(cArquivo, Empresas.Configuracoes[Emp].UnidadeFederativaCodigo);

                this.textBox_resultado.Text = validarXML.TipoArqXml.cRetornoTipoArq;

                if (validarXML.TipoArqXml.nRetornoTipoArq >= 1 && validarXML.TipoArqXml.nRetornoTipoArq <= SchemaXML.MaxID)
                {
                    this.edtTipoarquivo.Text = validarXML.TipoArqXml.cRetornoTipoArq;

                    //Assinar o arquivo XML copiado para a pasta TEMP
                    bool lValidar         = false;
                    AssinaturaDigital oAD = new AssinaturaDigital();
                    try
                    {
                        validarXML.EncryptAssinatura(cArquivo); //danasa: 12/2013
                        oAD.Assinar(cArquivo, Emp, Empresas.Configuracoes[Emp].UnidadeFederativaCodigo);
                        lValidar = true;
                    }
                    catch (Exception ex)
                    {
                        lValidar = false;
                        this.textBox_resultado.Text = "Ocorreu um erro ao tentar assinar o XML: \r\n\r\n" +
                                                      validarXML.TipoArqXml.cRetornoTipoArq + "\r\n" + ex.Message;
                    }

                    if (lValidar == true)
                    {
                        //Validar o arquivo
                        if (validarXML.TipoArqXml.nRetornoTipoArq >= 1 && validarXML.TipoArqXml.nRetornoTipoArq <= SchemaXML.MaxID)
                        {
                            ///danasa: 12/2013
                            validarXML.ValidarArqXML(cArquivo);
                            if (string.IsNullOrEmpty(validarXML.TipoArqXml.cArquivoSchema))
                            {
                                this.textBox_resultado.Text = "XML não possui schema de validação, sendo assim não é possível validar XML";
                            }
                            else if (validarXML.Retorno == 0)
                            {
                                this.textBox_resultado.Text = "Arquivo validado com sucesso!";
                            }
                            else
                            {
                                this.textBox_resultado.Text = "XML INCONSISTENTE!\r\n\r\n" + validarXML.RetornoString;
                            }
                        }
                        else
                        {
                            //this.textBox_tipoarq.Text = validarXML.TipoArqXml.cRetornoTipoArq;
                            this.textBox_resultado.Text = "XML INCONSISTENTE!\r\n\r\n" + validarXML.TipoArqXml.cRetornoTipoArq;
                        }
                    }
                }

                try
                {
                    if (wb == null)
                    {
                        wb                    = new WebBrowser();
                        wb.Parent             = this.metroTabPage2;
                        wb.Dock               = DockStyle.Fill;
                        wb.DocumentCompleted += webBrowser1_DocumentCompleted;
                    }
                    wb.Navigate(cArquivo);
                }
                catch {
                    webBrowser1_DocumentCompleted(null, null);
                }
            }
            catch (Exception ex)
            {
                this.textBox_resultado.Text = ex.Message + "\r\n" + ex.StackTrace;
            }
        }
示例#3
0
        private void btn_Validar_Click(object sender, EventArgs e)
        {
            LimparEPosicionarTC();

            try
            {
                if (this.edtFilename.Text == "" || !File.Exists(this.edtFilename.Text))
                {
                    this.textBox_resultado.Text = "Arquivo não encontrado.";
                    return;
                }

                //Copiar o arquivo XML para temporários para assinar e depois vou validar o que está nos temporários
                FileInfo oArquivo = new FileInfo(this.edtFilename.Text);
                string cArquivo = System.IO.Path.GetTempPath() + oArquivo.Name;

                oArqDestino = new FileInfo(cArquivo);

                oArquivo.CopyTo(cArquivo, true);

                //Remover atributo de somente leitura que pode gerar problemas no acesso do arquivo
                NFe.Service.TFunctions.RemoveSomenteLeitura(cArquivo);

                NFe.Service.TaskValidar val = new Service.TaskValidar();
                val.NomeArquivoXML = oArqDestino.FullName;
                val.Execute();

                //Detectar o tipo do arquivo
                NFe.Validate.ValidarXML validarXML = new NFe.Validate.ValidarXML(cArquivo, Empresas.Configuracoes[Emp].UnidadeFederativaCodigo);

                this.textBox_resultado.Text = validarXML.TipoArqXml.cRetornoTipoArq;

                if (validarXML.TipoArqXml.nRetornoTipoArq >= 1 && validarXML.TipoArqXml.nRetornoTipoArq <= SchemaXML.MaxID)
                {
                    this.edtTipoarquivo.Text = validarXML.TipoArqXml.cRetornoTipoArq;

                    //Assinar o arquivo XML copiado para a pasta TEMP
                    bool lValidar = false;
                    AssinaturaDigital oAD = new AssinaturaDigital();
                    try
                    {
                        validarXML.EncryptAssinatura(cArquivo); //danasa: 12/2013
                        oAD.Assinar(cArquivo, Emp, Empresas.Configuracoes[Emp].UnidadeFederativaCodigo);
                        lValidar = true;
                    }
                    catch (Exception ex)
                    {
                        lValidar = false;
                        this.textBox_resultado.Text = "Ocorreu um erro ao tentar assinar o XML: \r\n\r\n" +
                            validarXML.TipoArqXml.cRetornoTipoArq + "\r\n" + ex.Message;
                    }

                    if (lValidar == true)
                    {
                        //Validar o arquivo
                        if (validarXML.TipoArqXml.nRetornoTipoArq >= 1 && validarXML.TipoArqXml.nRetornoTipoArq <= SchemaXML.MaxID)
                        {
                            ///danasa: 12/2013
                            validarXML.ValidarArqXML(cArquivo);
                            if (string.IsNullOrEmpty(validarXML.TipoArqXml.cArquivoSchema))
                            {
                                this.textBox_resultado.Text = "XML não possui schema de validação, sendo assim não é possível validar XML";
                            }
                            else if (validarXML.Retorno == 0)
                            {
                                this.textBox_resultado.Text = "Arquivo validado com sucesso!";
                            }
                            else
                            {
                                this.textBox_resultado.Text = "XML INCONSISTENTE!\r\n\r\n" + validarXML.RetornoString;
                            }
                        }
                        else
                        {
                            //this.textBox_tipoarq.Text = validarXML.TipoArqXml.cRetornoTipoArq;
                            this.textBox_resultado.Text = "XML INCONSISTENTE!\r\n\r\n" + validarXML.TipoArqXml.cRetornoTipoArq;
                        }
                    }
                }

                try
                {
                    if (wb == null)
                    {
                        wb = new WebBrowser();
                        wb.Parent = this.metroTabPage2;
                        wb.Dock = DockStyle.Fill;
                        wb.DocumentCompleted += webBrowser1_DocumentCompleted;
                    }
                    wb.Navigate(cArquivo);
                }
                catch { 
                    webBrowser1_DocumentCompleted(null,null); 
                }
            }
            catch (Exception ex)
            {
                this.textBox_resultado.Text = ex.Message + "\r\n" + ex.StackTrace;
            }
        }