Beispiel #1
0
        private void NfeDadosMgs()
        {
            string _ano = daoUtil.GetDateServidor().ToString("yy");

            _id = Acesso.cUF + _ano + Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA) + _mod + _serie + _nnfini + _nnffim;
            try
            {
                XDocument xDados = new XDocument(new XElement(pf + "inutNFe", new XAttribute("versao", Acesso.versaoNFe),
                                                              new XElement(pf + "infInut", new XAttribute("Id", "ID" + _id),
                                                                           new XElement(pf + "tpAmb", Acesso.TP_AMB),
                                                                           new XElement(pf + "xServ", "INUTILIZAR"),
                                                                           new XElement(pf + "cUF", Acesso.cUF),
                                                                           new XElement(pf + "ano", daoUtil.GetDateServidor().ToString("yy")),
                                                                           new XElement(pf + "CNPJ", Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA)),
                                                                           new XElement(pf + "mod", _mod),
                                                                           new XElement(pf + "serie", Convert.ToInt16(_serie)),
                                                                           new XElement(pf + "nNFIni", Convert.ToInt32(_nnfini)),
                                                                           new XElement(pf + "nNFFin", Convert.ToInt32(_nnffim)),
                                                                           new XElement(pf + "xJust", _sjust))));

                _sPath = Pastas.PROTOCOLOS + _id + "_ped_inu.xml";
                xDados.Save(Pastas.PROTOCOLOS + _id + "_ped_inu.xml");
                belAssinaXml assina = new belAssinaXml();
                assina.Assinar(_sPath, "infInut", Acesso.cert_NFe);
                belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_NFE + "\\inutNFe_v2.00.xsd", _sPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string Cancelar()
        {
            try
            {
                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                nameSpaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                nameSpaces.Add("tipos", "http://*****:*****@"D:\Ret_CANC_LOTE_2805131157.xml";

                RetornoCancelamentoNFSe objretorno = SerializeClassToXml.DeserializeClasse<RetornoCancelamentoNFSe>(sPathRetConsultaCanc);

                string sMessageRetorno = TrataRetornoCancelamento(objretorno);
                return sMessageRetorno;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #3
0
        public string Cancelar()
        {
            try
            {
                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                nameSpaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                nameSpaces.Add("tipos", "http://*****:*****@"D:\Ret_CANC_LOTE_2805131157.xml";

                RetornoCancelamentoNFSe objretorno = SerializeClassToXml.DeserializeClasse <RetornoCancelamentoNFSe>(sPathRetConsultaCanc);

                string sMessageRetorno = TrataRetornoCancelamento(objretorno);
                return(sMessageRetorno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #4
0
        private string NfeDadosMsg3(string sJust)
        {
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;

            try
            {
                DataTable dt = BuscaProtocoloNFe(objPesquisa.sCD_NFSEQ);


                XNamespace xname = "http://www.portalfiscal.inf.br/nfe";
                XDocument  xdoc  = new XDocument(new XElement(xname + "cancNFe", new XAttribute("versao", Acesso.versaoNFe),
                                                              new XElement(xname + "infCanc", new XAttribute("Id", "ID" + objPesquisa.sCHAVENFE),
                                                                           new XElement(xname + "tpAmb", Acesso.TP_AMB),
                                                                           new XElement(xname + "xServ", "CANCELAR"),
                                                                           new XElement(xname + "chNFe", objPesquisa.sCHAVENFE),
                                                                           new XElement(xname + "nProt", dt.Rows[0]["cd_nprotnfe"].ToString()),
                                                                           new XElement(xname + "xJust", sJust))));
                string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.sCD_NFSEQ + "_ped-can.xml";
                xdoc.Save(sPath);

                belAssinaXml assinaCanc       = new belAssinaXml();
                string       sArquivoAssinado = assinaCanc.ConfigurarArquivo(sPath, "infCanc", Acesso.cert_NFe);


                string sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><procCancNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"2.00\">"
                                   + sArquivoAssinado.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "") + "</procCancNFe>";

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(sXMLfinal);
                if (File.Exists(sPath))
                {
                    File.Delete(sPath);
                }
                xDoc.Save(sPath);
                try
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CANC + "\\procEventoCancNFe_v1.00.xsd", sPath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return(sXMLfinal);
            }
            catch (Exception x)
            {
                throw new Exception(x.Message);
            }
        }
Beispiel #5
0
        private string MontaXmlConsultarSituacaoLoteRps(tcIdentificacaoPrestador objPrestador)
        {
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;

            try
            {
                XNamespace tipos        = "http://www.ginfes.com.br/tipos_v03.xsd";
                XNamespace pf           = "http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd";
                XContainer conPrestador = null;
                XContainer conProtocolo = null;

                XContainer conConsultarLoteRpsEnvio = (new XElement(pf + "ConsultarSituacaoLoteRpsEnvio", new XAttribute("xmlns", "http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd"),
                                                                    new XAttribute(XNamespace.Xmlns + "tipos", "http://www.ginfes.com.br/tipos_v03.xsd")));

                conPrestador = (new XElement(pf + "Prestador",
                                             new XElement(tipos + "Cnpj", objPrestador.Cnpj),
                                             ((objPrestador.InscricaoMunicipal != "") ? new XElement(tipos + "InscricaoMunicipal", objPrestador.InscricaoMunicipal) : null)));

                conProtocolo = new XElement(pf + "Protocolo", Protocolo);


                conConsultarLoteRpsEnvio.Add(conPrestador);
                conConsultarLoteRpsEnvio.Add(conProtocolo);
                belAssinaXml Assinatura = new belAssinaXml();
                string       sArquivo   = Assinatura.ConfigurarArquivo(conConsultarLoteRpsEnvio.ToString(), "Protocolo", Acesso.cert_NFs);



                XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                reader = new XmlValidatingReader(sArquivo, XmlNodeType.Element, context);

                myschema.Add("http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd", Pastas.SCHEMA_NFSE + "\\servico_consultar_situacao_lote_rps_envio_v03.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                {
                }


                return(sArquivo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #6
0
        private string MontaXmlCancelamentoV3(TcPedidoCancelamento objPedCanc)
        {
            try
            {
                XNamespace pf    = "http://www.ginfes.com.br/servico_cancelar_nfse_envio_v03.xsd";
                XNamespace tipos = "http://www.ginfes.com.br/tipos_v03.xsd";
                XNamespace xsi   = "http://www.w3.org/2001/XMLSchema-instance";

                XContainer conCancelarNfseEnvio = null;
                conCancelarNfseEnvio = (new XElement(pf + "CancelarNfseEnvio", new XAttribute(xsi + "schemaLocation", "http://www.ginfes.com.br/servico_cancelar_nfse_envio_v03.xsd"),
                                                     new XAttribute("xmlns", "http://www.ginfes.com.br/servico_cancelar_nfse_envio_v03.xsd"),
                                                     new XAttribute(XNamespace.Xmlns + "tipos", "http://www.ginfes.com.br/tipos_v03.xsd"),
                                                     new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                                                     new XElement(pf + "Pedido",
                                                                  new XElement(tipos + "InfPedidoCancelamento",
                                                                               new XElement(tipos + "IdentificacaoNfse",
                                                                                            new XElement(tipos + "Numero", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero),
                                                                                            new XElement(tipos + "Cnpj", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Cnpj),
                                                                                            new XElement(tipos + "InscricaoMunicipal", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.InscricaoMunicipal),
                                                                                            new XElement(tipos + "CodigoMunicipio", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.CodigoMunicipio)),
                                                                               new XElement(tipos + "CodigoCancelamento", objPedCanc.InfPedidoCancelamento.CodigoCancelamento)))));



                belAssinaXml Assinatura = new belAssinaXml();
                string       sArquivo   = Assinatura.ConfigurarArquivo(conCancelarNfseEnvio.ToString(), "tipos:InfPedidoCancelamento", Acesso.cert_NFs);

                DirectoryInfo dPastaData = new DirectoryInfo(Pastas.PROTOCOLOS + "\\Servicos");
                if (!dPastaData.Exists)
                {
                    dPastaData.Create();
                }
                XmlDocument xdocCanc = new XmlDocument();
                xdocCanc.LoadXml(sArquivo);
                string sCaminho = Pastas.PROTOCOLOS + "\\Servicos\\ped_canc_" + objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero + ".xml";
                xdocCanc.Save(sCaminho);


                // belValidaXml.ValidarXml("http://www.ginfes.com.br/servico_cancelar_nfse_envio.xsd", Pastas.SCHEMA_NFSE + "\\servico_cancelar_nfse_envio_v03.xsd", sCaminho);

                return("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + sArquivo);
            }
            catch (Exception ex)
            {
                return("");

                throw ex;
            }
        }
Beispiel #7
0
        private string NfeDadosMsg(string sJust)
        {
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;

            try
            {
                DataTable dt = BuscaProtocoloNFe(objPesquisa.sCD_NFSEQ);


                XNamespace xname = "http://www.portalfiscal.inf.br/nfe";
                XDocument  xdoc  = new XDocument(new XElement(xname + "cancNFe", new XAttribute("versao", Acesso.versaoNFe),
                                                              new XElement(xname + "infCanc", new XAttribute("Id", "ID" + objPesquisa.sCHAVENFE),
                                                                           new XElement(xname + "tpAmb", Acesso.TP_AMB),
                                                                           new XElement(xname + "xServ", "CANCELAR"),
                                                                           new XElement(xname + "chNFe", objPesquisa.sCHAVENFE),
                                                                           new XElement(xname + "nProt", dt.Rows[0]["cd_nprotnfe"].ToString()),
                                                                           new XElement(xname + "xJust", sJust))));
                string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.sCD_NFSEQ + "_ped-can.xml";
                xdoc.Save(sPath);

                belAssinaXml assinaCanc       = new belAssinaXml();
                string       sArquivoAssinado = assinaCanc.ConfigurarArquivo(sPath, "infCanc", Acesso.cert_NFe);

                StreamReader ler;
                ler = File.OpenText(sPath);

                XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                reader = new XmlValidatingReader(ler.ReadToEnd().ToString(), XmlNodeType.Element, context);

                myschema.Add("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_NFE + "\\cancNFe_v2.00.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                {
                }
                return(sArquivoAssinado);
            }
            catch (Exception x)
            {
                throw new Exception(x.Message);
            }
        }
Beispiel #8
0
        public void GeraXmlAndValida()
        {
            try
            {
                // Path do lote.
                if (!Directory.Exists(Pastas.ENVIO + this.enviMDFe.MDFe.infMDFe.Id.Substring(6, 4)))
                {
                    Directory.CreateDirectory(Pastas.ENVIO + this.enviMDFe.MDFe.infMDFe.Id.Substring(6, 4));
                }
                sPathLote = Pastas.ENVIO + this.enviMDFe.MDFe.infMDFe.Id.Substring(6, 4) + "\\Lote_" + this.enviMDFe.idLote + ".xml";

                if (File.Exists(sPathLote))
                {
                    File.Delete(sPathLote);
                }



                belAssinaXml            Assinatura = new belAssinaXml();
                XmlSerializerNamespaces ns         = new XmlSerializerNamespaces();
                ns.Add("", "http://www.portalfiscal.inf.br/mdfe");
                string xmlToAssinar = SerializeToString <TMDFe>(this.enviMDFe.MDFe, ns);
                string xmlAssinado  = Assinatura.ConfigurarArquivo(xmlToAssinar, "infMDFe", Acesso.cert_CTe);
                //Estou inserindo o enviNFe pois se eu transformar o arquivo xml assinado em xml e depois em texto usando um toString,
                //a assinatura se torna invalida. Então depois de assinado do trabalho em forma de texto como xml ateh envia-lo pra fazenda.
                xmlFinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><enviMDFe xmlns=\"http://www.portalfiscal.inf.br/mdfe\" versao=\"1.00\"><idLote>" +
                           this.enviMDFe.idLote.PadLeft(15, '0') + "</idLote>" + xmlAssinado + "</enviMDFe>";

                XElement  xnfe   = XElement.Parse(xmlFinal);
                XDocument xdSave = new XDocument(xnfe);

                xdSave.Save(sPathLote);

                belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/mdfe", Pastas.SCHEMA_MDFe + "\\enviMDFe_v1.00.xsd", sPathLote);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #9
0
        private string MontaXmlCancelamentoHomo(TcPedidoCancelamento objPedCanc)
        {
            try
            {
                XNamespace pf = "http://www.ginfes.com.br/servico_cancelar_nfse_envio";
                XContainer conCancelarNfseEnvio = null;
                conCancelarNfseEnvio = (new XElement(pf + "CancelarNfseEnvio", new XAttribute("xmlns", "http://www.ginfes.com.br/servico_cancelar_nfse_envio"),
                                                     new XElement(pf + "Prestador", new XElement(pf + "Cnpj", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Cnpj),
                                                                  ((objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.InscricaoMunicipal != "") ? new XElement(pf + "InscricaoMunicipal", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.InscricaoMunicipal) : null)),
                                                     new XElement(pf + "NumeroNfse", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero)));


                belAssinaXml Assinatura = new belAssinaXml();
                string       sArquivo   = Assinatura.ConfigurarArquivo(conCancelarNfseEnvio.ToString(), "NumeroNfse", Acesso.cert_NFs);

                DirectoryInfo dPastaData = new DirectoryInfo(Pastas.PROTOCOLOS + "\\Servicos");
                if (!dPastaData.Exists)
                {
                    dPastaData.Create();
                }
                XmlDocument xdocCanc = new XmlDocument();
                xdocCanc.LoadXml(sArquivo);
                string sCaminho = Pastas.PROTOCOLOS + "\\Servicos\\ped_canc_" + objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero + ".xml";
                xdocCanc.Save(sCaminho);


                //belValidaXml.ValidarXml("http://www.ginfes.com.br/servico_cancelar_nfse_envio.xsd", Pastas.SCHEMA_NFSE + "\\servico_cancelar_nfse_envio.xsd", sCaminho);

                sArquivo = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + sArquivo;

                return(sArquivo);
            }
            catch (Exception ex)
            {
                return("");

                throw ex;
            }
        }
Beispiel #10
0
        private string NFeDadosMsg2(belPesquisaNotas nota, string sJust, int iNumEvento)
        {
            string sVersao = "1.00";
            belCancelamento2 objcanc = new belCancelamento2();
            objcanc.idLote = nota.sCD_NFSEQ.PadLeft(15, '0');
            objcanc.versao = sVersao;
            Evento evento = new Evento();
            evento.versao = sVersao;
            evento.infEvento = new eventoInfEvento();

            evento.infEvento.nSeqEvento = iNumEvento.ToString(); // numero de evento
            evento.infEvento.Id = "ID" + evento.infEvento.tpEvento + nota.sCHAVENFE + evento.infEvento.nSeqEvento.PadLeft(2, '0');
            evento.infEvento.cOrgao = Convert.ToByte(Acesso.cUF);
            evento.infEvento.tpAmb = Convert.ToByte(Acesso.TP_AMB);
            evento.infEvento.CNPJ = Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA);
            evento.infEvento.chNFe = nota.sCHAVENFE;
            evento.infEvento.dhEvento = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss" + Acesso.FUSO);
            evento.infEvento.verEvento = sVersao;
            evento.infEvento.detEvento = new eventoInfEventoDetEvento();
            evento.infEvento.detEvento.versao = sVersao;
            evento.infEvento.detEvento.descEvento = "Cancelamento";
            evento.infEvento.detEvento.nProt = nota.cd_nprotnfe;
            evento.infEvento.detEvento.xJust = sJust;

            string sEvento = "";

            XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
            nameSpaces.Add("", "");
            nameSpaces.Add("", "http://www.portalfiscal.inf.br/nfe");


            XmlSerializer xs = new XmlSerializer(typeof(Evento));
            MemoryStream memory = new MemoryStream();
            XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);
            xs.Serialize(xmltext, evento, nameSpaces);
            UTF8Encoding encoding = new UTF8Encoding();
            sEvento = encoding.GetString(memory.ToArray());
            sEvento = sEvento.Substring(1);

            

            belAssinaXml Assinatura = new belAssinaXml();
            sEvento = Assinatura.ConfigurarArquivo(sEvento, "infEvento", Acesso.cert_NFe);

            string sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envEvento xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"1.00\"><idLote>" + nota.sCD_NFSEQ.PadLeft(15, '0')
                                + "</idLote>" + sEvento.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "") + "</envEvento>";

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sXMLfinal);
            string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.sCD_NFSEQ + "_ped-can.xml";
            if (File.Exists(sPath))
            {
                File.Delete(sPath);
            }
            xDoc.Save(sPath);
            try
            {
                belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CANC + "\\envEventoCancNFe_v1.00.xsd", sPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return sXMLfinal;
        }
Beispiel #11
0
        private string NfeDadosMsg3(string sJust)
        {
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;
            try
            {
                DataTable dt = BuscaProtocoloNFe(objPesquisa.sCD_NFSEQ);


                XNamespace xname = "http://www.portalfiscal.inf.br/nfe";
                XDocument xdoc = new XDocument(new XElement(xname + "cancNFe", new XAttribute("versao", Acesso.versaoNFe),
                                                   new XElement(xname + "infCanc", new XAttribute("Id", "ID" + objPesquisa.sCHAVENFE),
                                                       new XElement(xname + "tpAmb", Acesso.TP_AMB),
                                                       new XElement(xname + "xServ", "CANCELAR"),
                                                       new XElement(xname + "chNFe", objPesquisa.sCHAVENFE),
                                                       new XElement(xname + "nProt", dt.Rows[0]["cd_nprotnfe"].ToString()),
                                                       new XElement(xname + "xJust", sJust))));
                string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.sCD_NFSEQ + "_ped-can.xml";
                xdoc.Save(sPath);

                belAssinaXml assinaCanc = new belAssinaXml();
                string sArquivoAssinado = assinaCanc.ConfigurarArquivo(sPath, "infCanc", Acesso.cert_NFe);


                string sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><procCancNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"2.00\">"
                    + sArquivoAssinado.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "") + "</procCancNFe>";

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(sXMLfinal);
                if (File.Exists(sPath))
                {
                    File.Delete(sPath);
                }
                xDoc.Save(sPath);
                try
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CANC + "\\procEventoCancNFe_v1.00.xsd", sPath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return sXMLfinal;
            }
            catch (Exception x)
            {
                throw new Exception(x.Message);
            }
        }
        private void Serialize()
        {
            try
            {
                string sPathXml = GetFilePathLoteServico();
                if (File.Exists(sPathXml))
                {
                    File.Delete(sPathXml);
                }

                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                nameSpaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                nameSpaces.Add("tipos", "http://localhost:8080/WsNFe2/tp");
                nameSpaces.Add("ns1", "http://localhost:8080/WsNFe2/lote");

                //XmlTextWriter xmlTextWriter = new
                //XmlTextWriter(sPathXml, Encoding.UTF8);
                //xmlTextWriter.Formatting = Formatting.Indented;
                //XmlSerializer xs = new XmlSerializer(typeof(ReqEnvioLoteRPS));
                //xs.Serialize(xmlTextWriter, objLoteEnvio, nameSpaces);
                //xmlTextWriter.Close();

                foreach (LoteRPS item in objLoteEnvio.lote.RPS)
                {
                    item.bSerialize = true;
                }

                String XmlizedString = null;
                XmlSerializer x = new XmlSerializer(objLoteEnvio.GetType());
                MemoryStream memoryStream = new MemoryStream();
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                x.Serialize(xmlTextWriter, objLoteEnvio, nameSpaces);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                UTF8Encoding encoding = new UTF8Encoding();
                XmlizedString = encoding.GetString(memoryStream.ToArray());
                XmlizedString = XmlizedString.Substring(1);
                if (Acesso.TP_AMB_SERV == 1)
                {
                    belAssinaXml Assinatura = new belAssinaXml();
                    XmlizedString = Assinatura.ConfigurarArquivo(XmlizedString, "Lote", Acesso.cert_NFs);
                }

                this.xmlLote = new XmlDocument();
                this.xmlLote.LoadXml(XmlizedString);
                this.xmlLote.Save(sPathXml);
                this.ValidaXml(sPathXml);

                foreach (LoteRPS lot in objLoteEnvio.lote.RPS)
                {
                    sPathXml = GetFilePathMonthServico(false, lot.NumeroRPS);
                    if (File.Exists(sPathXml))
                    {
                        LoteRPS lotExistente = SerializeClassToXml.DeserializeClasse<LoteRPS>(sPathXml);

                        string sCD_NFSEQ = daoUtil.GetNFSEQbyNumeroRPS(lotExistente.NumeroRPS);

                        if (lot.CD_NFSEQ == sCD_NFSEQ)
                        {
                            File.Delete(sPathXml);
                        }
                        else
                        {
                            throw new Exception(string.Format("A Nota de sequencia '{0}' já esta utilizando o RPS de numero '{1}', é necessário envia-la para que o RPS possa ser incrementado.", sCD_NFSEQ, lotExistente.NumeroRPS));
                        }

                    }


                    SerializeClassToXml.SerializeClasse<LoteRPS>(lot, sPathXml);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #13
0
        private string NfeDadosMsg(string sJust)
        {
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;
            try
            {
                DataTable dt = BuscaProtocoloNFe(objPesquisa.sCD_NFSEQ);


                XNamespace xname = "http://www.portalfiscal.inf.br/nfe";
                XDocument xdoc = new XDocument(new XElement(xname + "cancNFe", new XAttribute("versao", Acesso.versaoNFe),
                                                   new XElement(xname + "infCanc", new XAttribute("Id", "ID" + objPesquisa.sCHAVENFE),
                                                       new XElement(xname + "tpAmb", Acesso.TP_AMB),
                                                       new XElement(xname + "xServ", "CANCELAR"),
                                                       new XElement(xname + "chNFe", objPesquisa.sCHAVENFE),
                                                       new XElement(xname + "nProt", dt.Rows[0]["cd_nprotnfe"].ToString()),
                                                       new XElement(xname + "xJust", sJust))));
                string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.sCD_NFSEQ + "_ped-can.xml";
                xdoc.Save(sPath);

                belAssinaXml assinaCanc = new belAssinaXml();
                string sArquivoAssinado = assinaCanc.ConfigurarArquivo(sPath, "infCanc", Acesso.cert_NFe);

                StreamReader ler;
                ler = File.OpenText(sPath);

                XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                reader = new XmlValidatingReader(ler.ReadToEnd().ToString(), XmlNodeType.Element, context);

                myschema.Add("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_NFE + "\\cancNFe_v2.00.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                { }
                return sArquivoAssinado;
            }
            catch (Exception x)
            {
                throw new Exception(x.Message);
            }
        }
Beispiel #14
0
        public void GeraXmlEnvio()
        {
            try
            {
                belAssinaXml Assinatura = new belAssinaXml();
                XDocument xdoc = new XDocument();
                XNamespace pf = "http://www.portalfiscal.inf.br/nfe";
                XContainer xContEnvEvento = new XElement(pf + "envEvento", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
                                                                    new XAttribute("versao", objEnvEvento.versao),
                                                                    new XElement(pf + "idLote", objEnvEvento.id));

                //List<string> ListaEventos = new List<string>();
                objXmlsSeparados = new Dictionary<string, string>();
                foreach (var obj in objEnvEvento.evento)
                {
                    XContainer xConEvento = new XElement(pf + "evento", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
                                                                   new XAttribute("versao", obj.versao),
                                                                   new XElement(pf + "infEvento", new XAttribute("Id", obj.idLote),
                                                                       new XElement(pf + "cOrgao", obj.infEvento.cOrgao),
                                                                       new XElement(pf + "tpAmb", obj.infEvento.tpAmb),
                                                                       new XElement(pf + (obj.infEvento.CNPJ != "" ? "CNPJ" : "CPF"), (obj.infEvento.CNPJ != "" ? obj.infEvento.CNPJ : obj.infEvento.CPF)),
                                                                       new XElement(pf + "chNFe", obj.infEvento.chNFe),
                                                                       new XElement(pf + "dhEvento", obj.infEvento.dhEvento),//-02:00
                                                                       new XElement(pf + "tpEvento", obj.infEvento.tpEvento),
                                                                       new XElement(pf + "nSeqEvento", obj.infEvento.nSeqEvento),
                                                                       new XElement(pf + "verEvento", obj.infEvento.verEvento),
                                                                       new XElement(pf + "detEvento", new XAttribute("versao", obj.versao),
                                                                                    new XElement(pf + "descEvento", obj.infEvento.detEvento.descEvento),
                                                                                    new XElement(pf + "xCorrecao", obj.infEvento.detEvento.xCorrecao),
                                                                                    new XElement(pf + "xCondUso", obj.infEvento.detEvento.xCondUso))));


                    objXmlsSeparados.Add(obj.infEvento.chNFe, Assinatura.ConfigurarArquivo(xConEvento.ToString(), "infEvento", Acesso.cert_NFe));
                }


                string sEventos = "";
                foreach (KeyValuePair<string, string> sInfEvento in objXmlsSeparados)
                {
                    sEventos += sInfEvento.Value;
                }

                sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envEvento xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"1.00\"><idLote>" + objEnvEvento.id.ToString()
                                + "</idLote>" + sEventos + "</envEvento>";

                StreamWriter sr = new StreamWriter(Pastas.CCe + "\\" + objEnvEvento.evento.FirstOrDefault().idLote + "_prevalida.xml");
                sr.Write(sXMLfinal);
                sr.Close();


                #region Valida_Xml


                XmlValidatingReader reader;
                try
                {
                    XmlSchemaCollection myschema = new XmlSchemaCollection();
                    myschema.Add("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CCE + "\\envCCe_v1.00.xsd");

                    XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
                    reader = new XmlValidatingReader(sXMLfinal, XmlNodeType.Element, context);
                    reader.ValidationType = ValidationType.Schema;
                    reader.Schemas.Add(myschema);
                    while (reader.Read())
                    {

                    }
                }
                catch (XmlException x)
                {
                    throw new Exception(x.Message);
                }
                catch (XmlSchemaException x)
                {
                    throw new Exception(x.Message);
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
Beispiel #15
0
        private void CarregaItensEventoCartaCorrecao(belPesquisaCCe objbelPesquisa)
        {
            try
            {
                if (Acesso.NM_RAMO != Acesso.BancoDados.TRANSPORTE)
                {
                    objEnvEvento        = new belEnvEvento();
                    objEnvEvento.versao = this._VERSAO;
                    objEnvEvento.id     = objbelPesquisa.CD_NRLANC;
                    belEvento objEvento = new belEvento();
                    objEvento.versao              = _VERSAO;
                    objEvento.infEvento           = new _infEvento();
                    objEvento.infEvento.CNPJ      = objbelPesquisa.CNPJ;
                    objEvento.infEvento.CPF       = objbelPesquisa.CPF;
                    objEvento.infEvento.dhEvento  = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss" + Acesso.FUSO);
                    objEvento.infEvento.verEvento = _VERSAO;
                    objEvento.infEvento.tpAmb     = Acesso.TP_AMB;
                    objEvento.infEvento.chNFe     = objbelPesquisa.CHNFE;
                    objEvento.infEvento.cOrgao    = Acesso.cUF.ToString();
                    objEvento.infEvento.detEvento = new _detEvento
                    {
                        versao = _VERSAO
                    };
                    objEvento.infEvento.detEvento.xCorrecao = BuscaCorrecoes(objbelPesquisa.CD_NRLANC);
                    objEvento.infEvento.nSeqEvento          = objbelPesquisa.QT_ENVIO + 1;
                    objEvento.idLote = "ID" + objEvento.infEvento.tpEvento + objEvento.infEvento.chNFe + objEvento.infEvento.nSeqEvento.ToString().PadLeft(2, '0');
                    objEnvEvento.evento.Add(objEvento);
                }
                else
                {
                    objEvCCeCTe = new TEvento();

                    objEvCCeCTe.versao               = Acesso.versaoCTe;
                    objEvCCeCTe.infEvento            = new  TEventoInfEvento();
                    objEvCCeCTe.infEvento.tpEvento   = "110110";
                    objEvCCeCTe.infEvento.nSeqEvento = (objbelPesquisa.QT_ENVIO + 1).ToString();// numero de evento
                    objEvCCeCTe.infEvento.Id         = "ID" + objEvCCeCTe.infEvento.tpEvento + objlItensPesquisa.FirstOrDefault().CHNFE + objEvCCeCTe.infEvento.nSeqEvento.ToString().PadLeft(2, '0');
                    //objEvCCeCTe.infEvento.Id = "ID" + objlItensPesquisa.FirstOrDefault().CHNFE;
                    objEvCCeCTe.infEvento.cOrgao    = Convert.ToByte(Acesso.cUF);
                    objEvCCeCTe.infEvento.tpAmb     = Acesso.TP_AMB == 1 ? TAmb.Item1 : TAmb.Item2;
                    objEvCCeCTe.infEvento.CNPJ      = Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA);
                    objEvCCeCTe.infEvento.chCTe     = objbelPesquisa.CHNFE;
                    objEvCCeCTe.infEvento.dhEvento  = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss");
                    objEvCCeCTe.infEvento.detEvento = new TEventoInfEventoDetEvento();
                    objEvCCeCTe.infEvento.detEvento.versaoEvento = Acesso.versaoCTe;

                    evCCeCTe objEvCanc = new evCCeCTe();
                    objEvCanc.descEvento  = evCCeCTeDescEvento.CartadeCorrecao;
                    objEvCanc.infCorrecao = new List <evCCeCTeInfCorrecao>();
                    objEvCanc.CD_NRLANC   = objbelPesquisa.CD_NRLANC;
                    foreach (DataRow item in BuscaCorrecoesCTe(objbelPesquisa.CD_NRLANC).Rows)
                    {
                        objEvCanc.infCorrecao.Add(new evCCeCTeInfCorrecao
                        {
                            grupoAlterado   = item["grupoAlterado"].ToString(),
                            campoAlterado   = item["campoAlterado"].ToString(),
                            valorAlterado   = item["valorAlterado"].ToString(),
                            nroItemAlterado = item["nroItemAlterado"].ToString(),
                        });
                    }
                    objEvCanc.xCondUso = evCCeCTeXCondUso.ACartadeCorreçãoédisciplinadapeloArt58BdoCONVÊNIOSINIEF0689FicapermitidaautilizaçãodecartadecorreçãopararegularizaçãodeerroocorridonaemissãodedocumentosfiscaisrelativosàprestaçãodeserviçodetransportedesdequeoerronãoestejarelacionadocomIasvariáveisquedeterminamovalordoimpostotaiscomobasedecálculoalíquotadiferençadepreçoquantidadevalordaprestaçãoIIacorreçãodedadoscadastraisqueimpliquemudançadoemitentetomadorremetenteoudodestinatárioIIIadatadeemissãooudesaída;
                    string xFile = Pastas.CCe + "\\" + objEvCanc.CD_NRLANC + "_prevalida.xml";
                    SerializeClassToXml.SerializeClasse <evCCeCTe>(objEvCanc, xFile);
                    XmlDocument xml = new XmlDocument();
                    xml.Load(xFile);
                    objEvCCeCTe.infEvento.detEvento.Any = (System.Xml.XmlElement)xml.DocumentElement;


                    XmlSerializer xs      = new XmlSerializer(typeof(TEvento));
                    MemoryStream  memory  = new MemoryStream();
                    XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);
                    xs.Serialize(xmltext, objEvCCeCTe);
                    UTF8Encoding encoding = new UTF8Encoding();
                    this.sXMLfinal = encoding.GetString(memory.ToArray());
                    this.sXMLfinal = this.sXMLfinal.Substring(1);

                    belAssinaXml Assinatura = new belAssinaXml();
                    this.sXMLfinal = Assinatura.ConfigurarArquivo(this.sXMLfinal, "infEvento", Acesso.cert_CTe);

                    xml = new XmlDocument();
                    xml.LoadXml(this.sXMLfinal);
                    string sPath = Pastas.PROTOCOLOS + "\\" + objbelPesquisa.CHNFE + "_ped-cce.xml";
                    if (File.Exists(sPath))
                    {
                        File.Delete(sPath);
                    }
                    xml.Save(sPath);
                    try
                    {
                        belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/cte", Pastas.SCHEMA_CTE + "\\eventoCTe_v2.00.xsd", sPath);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #16
0
        private string GetMsgDados()
        {
            string       sVersao      = "1.00";
            belEnvEvento objEnvEvento = new belEnvEvento();

            objEnvEvento.idLote = Util.GetNumeroNFe(this.xChaveNFe).PadLeft(15, '0');
            objEnvEvento.versao = sVersao;
            Evento evento = new Evento();

            evento.versao               = sVersao;
            evento.infEvento            = new eventoInfEvento();
            evento.infEvento.tpEvento   = this.xCodEvento;
            evento.infEvento.nSeqEvento = this.iNumEvento.ToString();

            evento.infEvento.nSeqEvento           = iNumEvento.ToString(); // numero de evento
            evento.infEvento.Id                   = "ID" + evento.infEvento.tpEvento + this.xChaveNFe + evento.infEvento.nSeqEvento.PadLeft(2, '0');
            evento.infEvento.cOrgao               = 91;
            evento.infEvento.tpAmb                = Convert.ToByte(Acesso.TP_AMB);
            evento.infEvento.CNPJ                 = Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA);
            evento.infEvento.chNFe                = this.xChaveNFe;
            evento.infEvento.dhEvento             = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss" + Acesso.FUSO);
            evento.infEvento.verEvento            = sVersao;
            evento.infEvento.detEvento            = new eventoInfEventoDetEvento();
            evento.infEvento.detEvento.versao     = sVersao;
            evento.infEvento.detEvento.descEvento = lTpEventos.FirstOrDefault(c => c.Key == this.xCodEvento).Value;
            evento.infEvento.detEvento.nProt      = this.xProt;
            evento.infEvento.detEvento.xJust      = this.xJust != "" ? this.xJust : null;

            string sEvento = "";

            XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();

            nameSpaces.Add("", "");
            nameSpaces.Add("", "http://www.portalfiscal.inf.br/nfe");


            XmlSerializer xs      = new XmlSerializer(typeof(Evento));
            MemoryStream  memory  = new MemoryStream();
            XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);

            xs.Serialize(xmltext, evento, nameSpaces);
            UTF8Encoding encoding = new UTF8Encoding();

            sEvento = encoding.GetString(memory.ToArray());
            sEvento = sEvento.Substring(1);



            belAssinaXml Assinatura = new belAssinaXml();

            sEvento = Assinatura.ConfigurarArquivo(sEvento, "infEvento", Acesso.cert_NFe);

            string sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envEvento xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"1.00\"><idLote>" + objEnvEvento.idLote
                               + "</idLote>" + sEvento.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "") + "</envEvento>";

            XmlDocument xDoc = new XmlDocument();

            xDoc.LoadXml(sXMLfinal);

            string sPath = "";

            if (this.tpEvento == tipoEvento.CANCELAMENTO)
            {
                sPath = Pastas.PROTOCOLOS + "\\" + objEnvEvento.idLote + "_ped-can.xml";
            }
            else if (tpEvento == tipoEvento.MANIFESTO)
            {
                sPath = Pastas.PROTOCOLOS + "\\" + objEnvEvento.idLote + "_" + this.xCodEvento + "_maniEvent.xml";
            }


            if (File.Exists(sPath))
            {
                File.Delete(sPath);
            }
            xDoc.Save(sPath);
            try
            {
                if (this.tpEvento == tipoEvento.CANCELAMENTO)
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CANC + "\\envEventoCancNFe_v1.00.xsd", sPath);
                }
                else if (tpEvento == tipoEvento.MANIFESTO)
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_MANIFESTACAO + "\\envConfRecebto_v1.00.xsd", sPath);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(sXMLfinal);
        }
Beispiel #17
0
        public string GerarXml(belPopulaObjetos objObjetos, int iNumLote)
        {
            try
            {
                List<string> listXml = new List<string>();

                DirectoryInfo dPastaMesAtual = null;
                DirectoryInfo dPastaEnvio = null;
                string sData = daoUtil.GetDateServidor().Date.ToString("dd-MM-yyyy");
                string sRecibo = "";

                XContainer CTe = null;
                XContainer infCte = null;
                XContainer ide = null;
                XContainer toma03 = null;
                XContainer toma4 = null;
                XContainer enderToma = null;
                XContainer emit = null;
                XContainer compl = null;
                XContainer enderEmit = null;
                XContainer rem = null;
                XContainer enderReme = null;
                XContainer infNF = null;
                XContainer infNFe = null;
                XContainer infOutros = null;
                XContainer exped = null;
                XContainer enderExped = null;
                XContainer receb = null;
                XContainer enderReceb = null;
                XContainer dest = null;
                XContainer enderDest = null;
                XContainer vPrest = null;
                XContainer Comp = null;
                XContainer imp = null;
                XContainer ICMS = null;
                XContainer ICMS00 = null;
                XContainer ICMS20 = null;
                XContainer ICMS45 = null;
                XContainer ICMS60 = null;
                XContainer ICMS90 = null;
                XContainer ICMSOutraUF = null;
                XContainer ICMSSN = null;

                XContainer infCTeNorm = null;
                XContainer seg = null;
                XContainer infModal = null;
                XContainer rodo = null;
                XContainer veic = null;
                XContainer prop = null;
                XContainer moto = null;


                #region Monta Cabecalho

                XNamespace pf = "http://www.portalfiscal.inf.br/cte";
                XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";



                #endregion

                for (int i = 0; i < objObjetos.objListaConhecimentos.Count; i++)
                {
                    CTe = new XElement(pf + "CTe");
                    belinfCte obj = objObjetos.objListaConhecimentos[i];

                    infCte = (new XElement(pf + "infCte", new XAttribute("versao", "2.00"),
                                                             new XAttribute("Id", obj.id)));

                    #region ide
                    ide = (new XElement(pf + "ide", new XElement(pf + "cUF", obj.ide.cUF),
                                                                new XElement(pf + "cCT", obj.ide.cCT),
                                                                new XElement(pf + "CFOP", obj.ide.CFOP),
                                                                new XElement(pf + "natOp", obj.ide.natOp),
                                                                new XElement(pf + "forPag", obj.ide.forPag),
                                                                new XElement(pf + "mod", obj.ide.mod),
                                                                new XElement(pf + "serie", obj.ide.serie),
                                                                new XElement(pf + "nCT", Convert.ToInt32(obj.ide.nCT)),
                                                                new XElement(pf + "dhEmi", (daoUtil.GetDateServidor()).ToString("yyyy-MM-ddTHH:MM:ss")),
                                                                new XElement(pf + "tpImp", obj.ide.tpImp),
                                                                new XElement(pf + "tpEmis", obj.ide.tpEmis),
                                                                new XElement(pf + "cDV", obj.ide.cDV),
                                                                new XElement(pf + "tpAmb", obj.ide.tpAmb),
                                                                new XElement(pf + "tpCTe", obj.ide.tpCTe),
                                                                new XElement(pf + "procEmi", obj.ide.procEmi),
                                                                new XElement(pf + "verProc", obj.ide.verProc),
                                                                new XElement(pf + "cMunEnv", obj.ide.cMunEnv),
                                                                new XElement(pf + "xMunEnv", obj.ide.xMunEnv),
                                                                new XElement(pf + "UFEnv", obj.ide.UFEnv),
                                                                new XElement(pf + "modal", obj.ide.modal),
                                                                new XElement(pf + "tpServ", obj.ide.tpServ),
                                                                new XElement(pf + "cMunIni", obj.ide.cMunIni),
                                                                new XElement(pf + "xMunIni", obj.ide.xMunIni),
                                                                new XElement(pf + "UFIni", obj.ide.UFIni),
                                                                new XElement(pf + "cMunFim", obj.ide.cMunFim),
                                                                new XElement(pf + "xMunFim", obj.ide.xMunFim),
                                                                new XElement(pf + "UFFim", obj.ide.UFFim),
                                                                new XElement(pf + "retira", obj.ide.retira)));


                    if (obj.ide.toma03 != null)
                    {
                        toma03 = (new XElement(pf + "toma03", new XElement(pf + "toma", obj.ide.toma03.toma)));
                        ide.Add(toma03);
                    }
                    else if (obj.ide.toma04 != null)
                    {
                        toma4 = (new XElement(pf + "toma4", new XElement(pf + "toma", obj.ide.toma04.toma),
                                                              obj.ide.toma04.CNPJ != "" ? new XElement(pf + "CNPJ", obj.ide.toma04.CNPJ) : null,
                                                              obj.ide.toma04.CPF != "" ? new XElement(pf + "CPF", obj.ide.toma04.CPF) : null,
                                                              obj.ide.toma04.IE != "" ? new XElement(pf + "IE", obj.ide.toma04.IE) : null,
                                                              new XElement(pf + "xNome", obj.ide.toma04.xNome),
                                                              obj.ide.toma04.xFant != "" ? new XElement(pf + "xFant", obj.ide.toma04.xFant) : null,
                                                              obj.ide.toma04.fone != "" ? new XElement(pf + "fone", obj.ide.toma04.fone) : null));

                        enderToma = (new XElement(pf + "enderToma", new XElement(pf + "xLgr", obj.ide.toma04.enderToma.xLgr),
                                                              new XElement(pf + "nro", obj.ide.toma04.enderToma.nro),
                                                              obj.ide.toma04.enderToma.xCpl != "" ? new XElement(pf + "xCpl", obj.ide.toma04.enderToma.xCpl) : null,
                                                              new XElement(pf + "xBairro", obj.ide.toma04.enderToma.xBairro),
                                                              new XElement(pf + "cMun", obj.ide.toma04.enderToma.cMun),
                                                              new XElement(pf + "xMun", obj.ide.toma04.enderToma.xMun),
                                                              obj.ide.toma04.enderToma.CEP != "" ? new XElement(pf + "CEP", obj.ide.toma04.enderToma.CEP) : null,
                                                              new XElement(pf + "UF", obj.ide.toma04.enderToma.UF),
                                                              obj.ide.toma04.enderToma.cPais != "" ? new XElement(pf + "cPais", obj.ide.toma04.enderToma.cPais) : null,
                                                              obj.ide.toma04.enderToma.xPais != null ? new XElement(pf + "xPais", obj.ide.toma04.enderToma.xPais) : null));

                        toma4.Add(enderToma);
                        ide.Add(toma4);
                    }


                    #endregion

                    #region Comp
                    // OS_ 30302
                    if (obj.compl.ObsCont.xTexto.Trim() != "")
                    {
                        compl = (obj.compl != null ? new XElement(pf + "compl", new XElement(pf + "ObsCont",
                                                                new XAttribute("xCampo", obj.compl.ObsCont.xCampo),
                                                                new XElement(pf + "xTexto", obj.compl.ObsCont.xTexto.Trim()))) : null);
                    }

                    #endregion

                    #region emit

                    emit = (new XElement(pf + "emit", new XElement(pf + "CNPJ", obj.emit.CNPJ),
                                                          new XElement(pf + "IE", obj.emit.IE),
                                                          new XElement(pf + "xNome", obj.emit.xNome),
                                                          obj.emit.xFant != "" ? new XElement(pf + "xFant", obj.emit.xFant) : null));

                    enderEmit = (new XElement(pf + "enderEmit", new XElement(pf + "xLgr", obj.emit.enderEmit.xLgr),
                                                                   new XElement(pf + "nro", obj.emit.enderEmit.nro),
                                                                   obj.emit.enderEmit.xCpl != "" ? new XElement(pf + "xCpl", obj.emit.enderEmit.xCpl) : null,
                                                                   new XElement(pf + "xBairro", obj.emit.enderEmit.xBairro),
                                                                   new XElement(pf + "cMun", obj.emit.enderEmit.cMun),
                                                                   new XElement(pf + "xMun", obj.emit.enderEmit.xMun),
                                                                   obj.emit.enderEmit.CEP != "" ? new XElement(pf + "CEP", obj.emit.enderEmit.CEP) : null,
                                                                   new XElement(pf + "UF", obj.emit.enderEmit.UF),
                                                                   obj.emit.enderEmit.fone != "" ? new XElement(pf + "fone", obj.emit.enderEmit.fone) : null));


                    emit.Add(enderEmit);
                    #endregion

                    #region rem
                    rem = (new XElement(pf + "rem", obj.rem.CNPJ != "" ? new XElement(pf + "CNPJ", obj.rem.CNPJ) : null,
                                                    obj.rem.CPF != "" ? new XElement(pf + "CPF", obj.rem.CPF) : null,
                                                          new XElement(pf + "IE", obj.rem.IE),
                                                          new XElement(pf + "xNome", obj.rem.xNome),
                                                          obj.rem.xFant != "" ? new XElement(pf + "xFant", obj.rem.xFant) : null,
                                                          obj.rem.fone != "" ? new XElement(pf + "fone", obj.rem.fone) : null));

                    enderReme = (new XElement(pf + "enderReme", new XElement(pf + "xLgr", obj.rem.enderReme.xLgr),
                                                                   new XElement(pf + "nro", obj.rem.enderReme.nro),
                                                                   obj.rem.enderReme.xCpl != "" ? new XElement(pf + "xCpl", obj.rem.enderReme.xCpl) : null,
                                                                   new XElement(pf + "xBairro", obj.rem.enderReme.xBairro),
                                                                   new XElement(pf + "cMun", obj.rem.enderReme.cMun),
                                                                   new XElement(pf + "xMun", obj.rem.enderReme.xMun),
                                                                   obj.rem.enderReme.CEP != "" ? new XElement(pf + "CEP", obj.rem.enderReme.CEP) : null,
                                                                   new XElement(pf + "UF", obj.rem.enderReme.UF),
                                                                   obj.rem.enderReme.cPais != "" ? new XElement(pf + "cPais", obj.rem.enderReme.cPais) : null,
                                                                   obj.rem.enderReme.xPais != "" ? new XElement(pf + "xPais", obj.rem.enderReme.xPais) : null));

                    rem.Add(enderReme);

                    // 1
                    //for (int j = 0; j < obj.rem.infNF.Count; j++)
                    //{
                    //    infNF = new XElement(pf + "infNF",
                    //               new XElement(pf + "mod", obj.rem.infNF[j].mod),
                    //               new XElement(pf + "serie", obj.rem.infNF[j].serie),
                    //               new XElement(pf + "nDoc", obj.rem.infNF[j].nDoc.Replace(",", ".")),
                    //               new XElement(pf + "dEmi", (Convert.ToDateTime(obj.rem.infNF[j].dEmi)).ToString("yyyy-MM-dd")),
                    //               new XElement(pf + "vBC", obj.rem.infNF[j].vBC.Replace(",", ".")),
                    //               new XElement(pf + "vICMS", obj.rem.infNF[j].vICMS.Replace(",", ".")),
                    //               new XElement(pf + "vBCST", obj.rem.infNF[j].vBCST.Replace(",", ".")),
                    //               new XElement(pf + "vST", obj.rem.infNF[j].vST.Replace(",", ".")),
                    //               new XElement(pf + "vProd", obj.rem.infNF[j].vProd.Replace(",", ".")),
                    //               new XElement(pf + "vNF", obj.rem.infNF[j].vNF.Replace(",", ".")),
                    //               new XElement(pf + "nCFOP", obj.rem.infNF[j].nCFOP.Replace(",", ".")));

                    //    rem.Add(infNF);
                    //}

                    // 2
                    //for (int nfe = 0; nfe < obj.rem.infNFe.Count; nfe++)
                    //{
                    //    infNFe = new XElement(pf + "infNFe", new XElement(pf + "chave", obj.rem.infNFe[nfe].chave));
                    //    rem.Add(infNFe);
                    //}
                    //3
                    //for (int j = 0; j < obj.rem.infOutros.Count; j++)
                    //{
                    //    infOutros = new XElement(pf + "infOutros",
                    //                            new XElement(pf + "tpDoc", obj.rem.infOutros[j].tpDoc),
                    //                            obj.rem.infOutros[j].descOutros != "" ? new XElement(pf + "descOutros", obj.rem.infOutros[j].descOutros) : null,
                    //                            new XElement(pf + "nDoc", obj.rem.infOutros[j].nDoc),
                    //                            new XElement(pf + "dEmi", Convert.ToDateTime(obj.rem.infOutros[j].dEmi).ToString("yyyy-MM-dd")),
                    //                            new XElement(pf + "vDocFisc", obj.rem.infOutros[j].vDocFisc.Replace(",", ".")));
                    //    rem.Add(infOutros);
                    //}

                    #endregion

                    #region exped

                    exped = null;

                    if (obj.exped != null)
                    {
                        exped = (new XElement(pf + "exped", obj.exped.CNPJ != "" ? new XElement(pf + "CNPJ", obj.exped.CNPJ) : null,
                                                        obj.exped.CPF != "" ? new XElement(pf + "CPF", obj.exped.CPF) : null,
                                                                              new XElement(pf + "IE", obj.exped.IE),
                                                                              new XElement(pf + "xNome", obj.exped.xNome),
                                                       obj.exped.fone != "" ? new XElement(pf + "fone", obj.exped.fone) : null));

                        enderExped = (new XElement(pf + "enderExped", new XElement(pf + "xLgr", obj.exped.enderExped.xLgr),
                                                                       new XElement(pf + "nro", obj.exped.enderExped.nro),
                                                                       obj.exped.enderExped.xCpl != "" ? new XElement(pf + "xCpl", obj.exped.enderExped.xCpl) : null,
                                                                       new XElement(pf + "xBairro", obj.exped.enderExped.xBairro),
                                                                       new XElement(pf + "cMun", obj.exped.enderExped.cMun),
                                                                       new XElement(pf + "xMun", obj.exped.enderExped.xMun),
                                                                       obj.exped.enderExped.CEP != "" ? new XElement(pf + "CEP", obj.exped.enderExped.CEP) : null,
                                                                       new XElement(pf + "UF", obj.exped.enderExped.UF),
                                                                       obj.exped.enderExped.cPais != "" ? new XElement(pf + "cPais", obj.exped.enderExped.cPais) : null,
                                                                       obj.exped.enderExped.xPais != "" ? new XElement(pf + "xPais", obj.exped.enderExped.xPais) : null));

                        exped.Add(enderExped);
                    }


                    #endregion

                    #region receb

                    receb = null;
                    if (obj.receb != null)
                    {
                        receb = (new XElement(pf + "receb", obj.receb.CNPJ != "" ? new XElement(pf + "CNPJ", obj.receb.CNPJ) : null,
                                                        obj.receb.CPF != "" ? new XElement(pf + "CPF", obj.receb.CPF) : null,
                                                                              new XElement(pf + "IE", obj.receb.IE),
                                                                              new XElement(pf + "xNome", obj.receb.xNome),
                                                       obj.receb.fone != "" ? new XElement(pf + "fone", obj.receb.fone) : null));

                        enderReceb = (new XElement(pf + "enderReceb", new XElement(pf + "xLgr", obj.receb.enderReceb.xLgr),
                                                                       new XElement(pf + "nro", obj.receb.enderReceb.nro),
                                                                       obj.receb.enderReceb.xCpl != "" ? new XElement(pf + "xCpl", obj.receb.enderReceb.xCpl) : null,
                                                                       new XElement(pf + "xBairro", obj.receb.enderReceb.xBairro),
                                                                       new XElement(pf + "cMun", obj.receb.enderReceb.cMun),
                                                                       new XElement(pf + "xMun", obj.receb.enderReceb.xMun),
                                                                       obj.receb.enderReceb.CEP != "" ? new XElement(pf + "CEP", obj.receb.enderReceb.CEP) : null,
                                                                       new XElement(pf + "UF", obj.receb.enderReceb.UF),
                                                                       obj.receb.enderReceb.cPais != "" ? new XElement(pf + "cPais", obj.receb.enderReceb.cPais) : null,
                                                                       obj.receb.enderReceb.xPais != "" ? new XElement(pf + "xPais", obj.receb.enderReceb.xPais) : null));

                        receb.Add(enderReceb);
                    }


                    #endregion

                    #region dest

                    dest = (new XElement(pf + "dest", obj.dest.CNPJ != "" ? new XElement(pf + "CNPJ", obj.dest.CNPJ) : null,
                                                      obj.dest.CPF != "" ? new XElement(pf + "CPF", obj.dest.CPF) : null,
                                                      obj.dest.IE != "" ? new XElement(pf + "IE", obj.dest.IE) : null,
                                                                          new XElement(pf + "xNome", obj.dest.xNome),
                                                      obj.dest.fone != "" ? new XElement(pf + "fone", obj.dest.fone) : null,
                                                      obj.dest.ISUF != "" ? new XElement(pf + "ISUF", obj.dest.ISUF) : null));


                    enderDest = (new XElement(pf + "enderDest", new XElement(pf + "xLgr", obj.dest.enderDest.xLgr),
                                                                   new XElement(pf + "nro", obj.dest.enderDest.nro),
                                                                   obj.dest.enderDest.xCpl != "" ? new XElement(pf + "xCpl", obj.dest.enderDest.xCpl) : null,
                                                                   new XElement(pf + "xBairro", obj.dest.enderDest.xBairro),
                                                                   new XElement(pf + "cMun", obj.dest.enderDest.cMun),
                                                                   new XElement(pf + "xMun", obj.dest.enderDest.xMun),
                                                                   obj.dest.enderDest.CEP != "" ? new XElement(pf + "CEP", obj.dest.enderDest.CEP) : null,
                                                                   new XElement(pf + "UF", obj.dest.enderDest.UF),
                                                                   obj.dest.enderDest.cPais != "" ? new XElement(pf + "cPais", obj.dest.enderDest.cPais) : null,
                                                                   obj.dest.enderDest.xPais != "" ? new XElement(pf + "xPais", obj.dest.enderDest.xPais) : null));

                    dest.Add(enderDest);
                    #endregion

                    #region vPrest

                    vPrest = (new XElement(pf + "vPrest", new XElement(pf + "vTPrest", obj.vPrest.vTPrest.Replace(",", ".")),
                                                          new XElement(pf + "vRec", obj.vPrest.vRec.Replace(",", "."))));

                    for (int j = 0; j < obj.vPrest.Comp.Count; j++)
                    {
                        if (Convert.ToDecimal(obj.vPrest.Comp[j].vComp.Replace(".", ",")) > 0)
                        {
                            Comp = new XElement(pf + "Comp", new XElement(pf + "xNome", obj.vPrest.Comp[j].xNome),
                                                             new XElement(pf + "vComp", Convert.ToDecimal(obj.vPrest.Comp[j].vComp.Replace(".", ",")).ToString("#0.00").Replace(",", ".")));
                            vPrest.Add(Comp);
                        }
                    }

                    #endregion

                    #region imp

                    imp = new XElement(pf + "imp");
                    //ICMS = new XElement(pf + "ICMS", new XElement(pf + "vTotTrib", "200.06"));
                    ICMS = new XElement(pf + "ICMS");

                    if (obj.imp.ICMS.ICMS00 != null)
                    {
                        belICMS00 objICMS00 = obj.imp.ICMS.ICMS00;
                        ICMS00 = (new XElement(pf + "ICMS00", new XElement(pf + "CST", objICMS00.CST),
                                                                new XElement(pf + "vBC", objICMS00.vBC),
                                                                new XElement(pf + "pICMS", objICMS00.pICMS),
                                                                new XElement(pf + "vICMS", objICMS00.vICMS)));
                        ICMS.Add(ICMS00);
                    }
                    else if (obj.imp.ICMS.ICMS20 != null)
                    {
                        belICMS20 objICMS20 = obj.imp.ICMS.ICMS20;
                        ICMS20 = (new XElement(pf + "ICMS20", new XElement(pf + "CST", objICMS20.CST),
                                                               new XElement(pf + "pRedBC", objICMS20.pRedBC),
                                                               new XElement(pf + "vBC", objICMS20.vBC),
                                                               new XElement(pf + "pICMS", objICMS20.pICMS),
                                                               new XElement(pf + "vICMS", objICMS20.vICMS)));
                        ICMS.Add(ICMS20);
                    }
                    else if (obj.imp.ICMS.ICMS45 != null)
                    {
                        ICMS45 = (new XElement(pf + "ICMS45", new XElement(pf + "CST", obj.imp.ICMS.ICMS45.CST)));
                        ICMS.Add(ICMS45);
                    }
                    else if (obj.imp.ICMS.ICMS60 != null)
                    {
                        belICMS60 objICMS60 = obj.imp.ICMS.ICMS60;
                        ICMS60 = (new XElement(pf + "ICMS60", new XElement(pf + "CST", objICMS60.CST),
                                                                new XElement(pf + "vBCSTRet", objICMS60.vBCSTRet),
                                                                new XElement(pf + "vICMSSTRet", objICMS60.vICMSSTRet),
                                                                new XElement(pf + "pICMSSTRet", objICMS60.pICMSSTRet)));
                        ICMS.Add(ICMS60);
                    }
                    else if (obj.imp.ICMS.ICMS90 != null)
                    {
                        belICMS90 objICMS90 = obj.imp.ICMS.ICMS90;
                        ICMS60 = (new XElement(pf + "ICMS90", new XElement(pf + "CST", objICMS90.CST),
                                                                new XElement(pf + "vBC", objICMS90.vBC),
                                                                new XElement(pf + "pICMS", objICMS90.pICMS),
                                                                new XElement(pf + "vICMS", objICMS90.vICMS)));
                        ICMS.Add(ICMS90);
                    }
                    else if (obj.imp.ICMS.ICMSOutraUF != null)
                    {
                        belICMSOutraUF objICMSOutraUF = obj.imp.ICMS.ICMSOutraUF;
                        ICMSOutraUF = (new XElement(pf + "ICMSOutraUF", new XElement(pf + "CST", objICMSOutraUF.CST),
                                                                 objICMSOutraUF.pRedBCOutraUF != "" ? new XElement(pf + "pRedBCOutraUF", objICMSOutraUF.pRedBCOutraUF) : null,
                                                                 new XElement(pf + "vBCOutraUF", objICMSOutraUF.vBCOutraUF),
                                                                 new XElement(pf + "pICMSOutraUF", objICMSOutraUF.pICMSOutraUF),
                                                                 new XElement(pf + "vICMSOutraUF", objICMSOutraUF.vICMSOutraUF)));
                        ICMS.Add(ICMSOutraUF);
                    }
                    else if (obj.imp.ICMS.ICMSSN != null)
                    {
                        ICMSSN = (new XElement(pf + "ICMSSN", new XElement(pf + "indSN", "1")));
                        ICMS.Add(ICMSSN);
                    }

                    imp.Add(ICMS);
                    #endregion

                    #region infCTeNorm

                    infCTeNorm = new XElement(pf + "infCTeNorm", new XElement(pf + "infCarga",
                                                                                  new XElement(pf + "vCarga", obj.infCTeNorm.infCarga.vCarga),
                                                                                  new XElement(pf + "proPred", obj.infCTeNorm.infCarga.proPred),
                                                                                  obj.infCTeNorm.infCarga.xOutCat.Trim() != "" ? new XElement(pf + "xOutCat", obj.infCTeNorm.infCarga.xOutCat.Trim()) : null,
                                                                                  obj.infCTeNorm.infCarga.infQ != null ?
                                                                                  (from c in obj.infCTeNorm.infCarga.infQ
                                                                                   select new XElement(pf + "infQ", new XElement(pf + "cUnid", c.cUnid),
                                                                                                               new XElement(pf + "tpMed", c.tpMed),
                                                                                                               new XElement(pf + "qCarga", retornaqCarga(c.qCarga)))) : null),
                                                                 (obj.infCTeNorm.infDoc != null ? new XElement(pf + "infDoc",
                                                                   (obj.infCTeNorm.infDoc.infNF.Count > 0 ?
                                                                       (from c in obj.infCTeNorm.infDoc.infNF  //2.0
                                                                        select new XElement(pf + "infNF",
                                                                        new XElement(pf + "mod", c.mod),
                                                                        new XElement(pf + "serie", c.serie),
                                                                        new XElement(pf + "nDoc", c.nDoc.Replace(",", ".")),
                                                                        new XElement(pf + "dEmi", (Convert.ToDateTime(c.dEmi)).ToString("yyyy-MM-dd")),
                                                                        new XElement(pf + "vBC", c.vBC.Replace(",", ".")),
                                                                        new XElement(pf + "vICMS", c.vICMS.Replace(",", ".")),
                                                                        new XElement(pf + "vBCST", c.vBCST.Replace(",", ".")),
                                                                        new XElement(pf + "vST", c.vST.Replace(",", ".")),
                                                                        new XElement(pf + "vProd", c.vProd.Replace(",", ".")),
                                                                        new XElement(pf + "vNF", c.vNF.Replace(",", ".")),
                                                                        new XElement(pf + "nCFOP", c.nCFOP.Replace(",", ".")))) : null),
                                                                    (obj.infCTeNorm.infDoc.infNFe.Count > 0 ?
                                                                        (from c in obj.infCTeNorm.infDoc.infNFe //2.0                                                                             select 
                                                                         select new XElement(pf + "infNFe", new XElement(pf + "chave", c.chave))) : null),
                                                                     (obj.infCTeNorm.infDoc.infOutros.Count > 0 ? //2.0
                                                                     (from c in obj.infCTeNorm.infDoc.infOutros
                                                                      select new XElement(pf + "infOutros",
                                                                           new XElement(pf + "tpDoc", c.tpDoc),
                                                                            c.descOutros != "" ? new XElement(pf + "descOutros", c.descOutros) : null,
                                                                            new XElement(pf + "nDoc", c.nDoc),
                                                                            new XElement(pf + "dEmi", Convert.ToDateTime(c.dEmi).ToString("yyyy-MM-dd")),
                                                                            new XElement(pf + "vDocFisc", c.vDocFisc.Replace(",", ".")))) : null)) : null),
                                                                 ((obj.ide.tpCTe == 0 || obj.ide.tpCTe == 3) && (obj.ide.tpServ > 0)) ? new XElement(pf + "docAnt",
                                                                      new XElement(pf + "emiDocAnt",
                                                                            obj.rem.CNPJ != "" ? new XElement(pf + "CNPJ", obj.rem.CNPJ) : null,
                                                                            obj.rem.CPF != "" ? new XElement(pf + "CPF", obj.rem.CPF) : null,
                                                                            new XElement(pf + "IE", obj.rem.IE),
                                                                            new XElement(pf + "UF", obj.rem.enderReme.UF),
                                                                            new XElement(pf + "xNome", obj.rem.xNome.Trim()),
                                                                      new XElement(pf + "idDocAnt",
                                                                          (from c in obj.infCTeNorm.infDoc.infNFe
                                                                           select new XElement(pf + "idDocAntEle",
                                                                           new XElement(pf + "chave", c.chave)
                                                                     ))))) : null);



                    seg = new XElement(pf + "seg", new XElement(pf + "respSeg", obj.infCTeNorm.seg.respSeg),
                                                            new XElement(pf + "nApol", obj.infCTeNorm.seg.nApol));
                    infCTeNorm.Add(seg);


                    #region Modal Rodoviario

                    infModal = new XElement(pf + "infModal", new XAttribute("versaoModal", "2.00"));

                    rodo = new XElement(pf + "rodo", new XElement(pf + "RNTRC", obj.infCTeNorm.rodo.RNTRC),
                                                              new XElement(pf + "dPrev", Convert.ToDateTime(obj.infCTeNorm.rodo.dPrev).ToString("yyyy-MM-dd")),
                                                              new XElement(pf + "lota", obj.infCTeNorm.rodo.lota));



                    for (int v = 0; v < obj.infCTeNorm.rodo.veic.Count; v++)
                    {

                        veic = new XElement(pf + "veic", new XElement(pf + "RENAVAM", obj.infCTeNorm.rodo.veic[v].RENAVAM),
                                              new XElement(pf + "placa", obj.infCTeNorm.rodo.veic[v].placa),
                                              new XElement(pf + "tara", obj.infCTeNorm.rodo.veic[v].tara),
                                              new XElement(pf + "capKG", obj.infCTeNorm.rodo.veic[v].capKG),
                                              new XElement(pf + "capM3", obj.infCTeNorm.rodo.veic[v].capM3),
                                              new XElement(pf + "tpProp", obj.infCTeNorm.rodo.veic[v].tpProp),
                                              new XElement(pf + "tpVeic", obj.infCTeNorm.rodo.veic[v].tpVeic),
                                              new XElement(pf + "tpRod", obj.infCTeNorm.rodo.veic[v].tpRod),
                                              new XElement(pf + "tpCar", obj.infCTeNorm.rodo.veic[v].tpCar),
                                              new XElement(pf + "UF", obj.infCTeNorm.rodo.veic[v].UF));

                        if (obj.infCTeNorm.rodo.veic[v].prop != null)
                        {
                            prop = new XElement(pf + "prop", new XElement(pf + (obj.infCTeNorm.rodo.veic[v].prop.CPFCNPJ.Length == 11 ? "CPF" : "CNPJ"), obj.infCTeNorm.rodo.veic[v].prop.CPFCNPJ),
                                     new XElement(pf + "RNTRC", obj.infCTeNorm.rodo.veic[v].prop.RNTRC),
                                                  new XElement(pf + "xNome", obj.infCTeNorm.rodo.veic[v].prop.xNome),
                                                  new XElement(pf + "IE", obj.infCTeNorm.rodo.veic[v].prop.IE),
                                                  new XElement(pf + "UF", obj.infCTeNorm.rodo.veic[v].prop.UF),
                                                  new XElement(pf + "tpProp", obj.infCTeNorm.rodo.veic[v].prop.tpProp));

                            veic.Add(prop);
                        }
                        rodo.Add(veic);
                    }
                    if (obj.infCTeNorm.rodo.moto != null)
                    {
                        moto = new XElement(pf + "moto", new XElement(pf + "xNome", obj.infCTeNorm.rodo.moto.xNome),
                               new XElement(pf + "CPF", obj.infCTeNorm.rodo.moto.CPF));
                        rodo.Add(moto);
                    }

                    #endregion

                    infModal.Add(rodo);
                    infCTeNorm.Add(infModal);


                    #endregion

                    #region Monta CTe

                    infCte.Add(ide);
                    infCte.Add(compl);
                    infCte.Add(emit);
                    infCte.Add(rem);
                    infCte.Add(exped);
                    infCte.Add(receb);
                    infCte.Add(dest);
                    infCte.Add(vPrest);
                    infCte.Add(imp);
                    infCte.Add(infCTeNorm);
                    CTe.Add(infCte);

                    #endregion

                    #region Assinatura
                    belAssinaXml Assinatura = new belAssinaXml();
                    string sCte = Assinatura.ConfigurarArquivo(CTe.ToString(), "infCte", Acesso.cert_CTe);
                    listXml.Add(sCte);

                    #endregion

                    #region SalvaCteIndividual

                    sCte = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + sCte;
                    string sNumCte = obj.ide.nCT.PadLeft(6, '0');

                    if (Acesso.TP_EMIS == 2)
                    {
                        dPastaEnvio = new DirectoryInfo(Pastas.CONTINGENCIA);
                    }
                    else
                    {
                        dPastaEnvio = new DirectoryInfo(Pastas.ENVIO);
                    }
                    if (!dPastaEnvio.Exists) { dPastaEnvio.Create(); }

                    dPastaMesAtual = new DirectoryInfo(dPastaEnvio + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2));
                    if (!dPastaMesAtual.Exists) { dPastaMesAtual.Create(); }
                    File.WriteAllText(dPastaMesAtual.ToString() + "\\" + "Cte_" + sNumCte + ".xml", sCte);

                    #endregion


                }

                #region Salva Lote

                string sXml = "";
                foreach (var xml in listXml)
                {
                    sXml = sXml + xml;
                }

                string sXmlCte = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><enviCTe versao=\"2.00\" " +
                                 "xsi:schemaLocation=\"http://www.portalfiscal.inf.br/cte enviCte_v1.04.xsd\" " +
                                 "xmlns=\"http://www.portalfiscal.inf.br/cte\" xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" " +
                                 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><idLote>" + iNumLote + "</idLote>" + sXml + "</enviCTe>");

                string sPathLote = dPastaEnvio.ToString() + "\\" + "Cte_Lote_" + iNumLote + "_" + sData + ".xml";
                StreamWriter sw = new StreamWriter(sPathLote);
                sw.Write(sXmlCte);
                sw.Close();


                #endregion

                #region Valida_Xml
                belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/cte", Pastas.SCHEMA_CTE + "\\enviCTe_v2.00.xsd", sPathLote);

                //belGlobais getschema = new belGlobais();
                //XmlSchemaCollection myschema = new XmlSchemaCollection();

                //XmlValidatingReader reader;

                //try
                //{
                //    XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                //    reader = new XmlValidatingReader(sXmlCte.ToString(), XmlNodeType.Element, context);

                //    myschema.Add("http://www.portalfiscal.inf.br/cte", belStaticPastas.SCHEMA_CTE + "\\enviCte_v1.04.xsd");

                //    reader.ValidationType = ValidationType.Schema;

                //    reader.Schemas.Add(myschema);

                //    while (reader.Read())
                //    {
                //    }

                //}
                //catch (XmlException x)
                //{
                //    throw new Exception(x.Message);
                //}
                //catch (XmlSchemaException x)
                //{
                //    throw new Exception(x.Message);
                //}

                #endregion

                #region Envia e Busca Recibo Lote WebService

                if (Acesso.TP_EMIS == 1 || Acesso.TP_EMIS == 6) // modo normal  // os_svc
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(dPastaEnvio.ToString() + "\\" + "Cte_Lote_" + iNumLote + "_" + sData + ".xml");


                    //busca retorno
                    string sRetorno = TransmitirLote(doc);
                    sRecibo = BuscaReciboRetornoEnvio(sRetorno);
                }

                #endregion

                return sRecibo;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void Serialize()
        {
            try
            {
                string sPathXml = GetFilePathLoteServico();
                if (File.Exists(sPathXml))
                {
                    File.Delete(sPathXml);
                }

                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                nameSpaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                nameSpaces.Add("tipos", "http://localhost:8080/WsNFe2/tp");
                nameSpaces.Add("ns1", "http://localhost:8080/WsNFe2/lote");

                //XmlTextWriter xmlTextWriter = new
                //XmlTextWriter(sPathXml, Encoding.UTF8);
                //xmlTextWriter.Formatting = Formatting.Indented;
                //XmlSerializer xs = new XmlSerializer(typeof(ReqEnvioLoteRPS));
                //xs.Serialize(xmlTextWriter, objLoteEnvio, nameSpaces);
                //xmlTextWriter.Close();

                foreach (LoteRPS item in objLoteEnvio.lote.RPS)
                {
                    item.bSerialize = true;
                }

                String        XmlizedString = null;
                XmlSerializer x             = new XmlSerializer(objLoteEnvio.GetType());
                MemoryStream  memoryStream  = new MemoryStream();
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                x.Serialize(xmlTextWriter, objLoteEnvio, nameSpaces);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                UTF8Encoding encoding = new UTF8Encoding();
                XmlizedString = encoding.GetString(memoryStream.ToArray());
                XmlizedString = XmlizedString.Substring(1);
                if (Acesso.TP_AMB_SERV == 1)
                {
                    belAssinaXml Assinatura = new belAssinaXml();
                    XmlizedString = Assinatura.ConfigurarArquivo(XmlizedString, "Lote", Acesso.cert_NFs);
                }

                this.xmlLote = new XmlDocument();
                this.xmlLote.LoadXml(XmlizedString);
                this.xmlLote.Save(sPathXml);
                this.ValidaXml(sPathXml);

                foreach (LoteRPS lot in objLoteEnvio.lote.RPS)
                {
                    sPathXml = GetFilePathMonthServico(false, lot.NumeroRPS);
                    if (File.Exists(sPathXml))
                    {
                        LoteRPS lotExistente = SerializeClassToXml.DeserializeClasse <LoteRPS>(sPathXml);

                        string sCD_NFSEQ = daoUtil.GetNFSEQbyNumeroRPS(lotExistente.NumeroRPS);

                        if (lot.CD_NFSEQ == sCD_NFSEQ)
                        {
                            File.Delete(sPathXml);
                        }
                        else
                        {
                            throw new Exception(string.Format("A Nota de sequencia '{0}' já esta utilizando o RPS de numero '{1}', é necessário envia-la para que o RPS possa ser incrementado.", sCD_NFSEQ, lotExistente.NumeroRPS));
                        }
                    }


                    SerializeClassToXml.SerializeClasse <LoteRPS>(lot, sPathXml);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #19
0
        private string NFeDadosMsg2(belPesquisaNotas nota, string sJust, int iNumEvento)
        {
            string           sVersao = "1.00";
            belCancelamento2 objcanc = new belCancelamento2();

            objcanc.idLote = nota.sCD_NFSEQ.PadLeft(15, '0');
            objcanc.versao = sVersao;
            Evento evento = new Evento();

            evento.versao    = sVersao;
            evento.infEvento = new eventoInfEvento();

            evento.infEvento.nSeqEvento           = iNumEvento.ToString(); // numero de evento
            evento.infEvento.Id                   = "ID" + evento.infEvento.tpEvento + nota.sCHAVENFE + evento.infEvento.nSeqEvento.PadLeft(2, '0');
            evento.infEvento.cOrgao               = Convert.ToByte(Acesso.cUF);
            evento.infEvento.tpAmb                = Convert.ToByte(Acesso.TP_AMB);
            evento.infEvento.CNPJ                 = Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA);
            evento.infEvento.chNFe                = nota.sCHAVENFE;
            evento.infEvento.dhEvento             = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss" + Acesso.FUSO);
            evento.infEvento.verEvento            = sVersao;
            evento.infEvento.detEvento            = new eventoInfEventoDetEvento();
            evento.infEvento.detEvento.versao     = sVersao;
            evento.infEvento.detEvento.descEvento = "Cancelamento";
            evento.infEvento.detEvento.nProt      = nota.cd_nprotnfe;
            evento.infEvento.detEvento.xJust      = sJust;

            string sEvento = "";

            XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();

            nameSpaces.Add("", "");
            nameSpaces.Add("", "http://www.portalfiscal.inf.br/nfe");


            XmlSerializer xs      = new XmlSerializer(typeof(Evento));
            MemoryStream  memory  = new MemoryStream();
            XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);

            xs.Serialize(xmltext, evento, nameSpaces);
            UTF8Encoding encoding = new UTF8Encoding();

            sEvento = encoding.GetString(memory.ToArray());
            sEvento = sEvento.Substring(1);



            belAssinaXml Assinatura = new belAssinaXml();

            sEvento = Assinatura.ConfigurarArquivo(sEvento, "infEvento", Acesso.cert_NFe);

            string sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envEvento xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"1.00\"><idLote>" + nota.sCD_NFSEQ.PadLeft(15, '0')
                               + "</idLote>" + sEvento.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "") + "</envEvento>";

            XmlDocument xDoc = new XmlDocument();

            xDoc.LoadXml(sXMLfinal);
            string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.sCD_NFSEQ + "_ped-can.xml";

            if (File.Exists(sPath))
            {
                File.Delete(sPath);
            }
            xDoc.Save(sPath);
            try
            {
                belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CANC + "\\envEventoCancNFe_v1.00.xsd", sPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(sXMLfinal);
        }
Beispiel #20
0
        private XmlDocument Exec_Cosulta(string sRec)
        {
            try
            {
                string snfeDadosMsg = NfeDadosMsg(sRec);

                belAssinaXml Assina      = new belAssinaXml();
                string       sRet        = string.Empty;
                string       sXmlRetorno = string.Empty;

                if (Acesso.TP_EMIS == 6)
                {
                    if (Acesso.TP_AMB == 1)
                    {
                        HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.nfeCabecMsg();
                        cabec.cUF            = Acesso.cUF.ToString();
                        cabec.versaoDados    = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                    else
                    {
                        HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.nfeCabecMsg();
                        cabec.cUF            = Acesso.cUF.ToString();
                        cabec.versaoDados    = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                }
                else if (Acesso.TP_EMIS == 3)
                {
                    #region SCAN
                    if (Acesso.TP_AMB == 1)
                    {
                        HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.nfeCabecMsg();
                        cabec.cUF            = Acesso.cUF.ToString();
                        cabec.versaoDados    = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                    else if (Acesso.TP_AMB == 2)
                    {
                        HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.nfeCabecMsg();
                        cabec.cUF            = Acesso.cUF.ToString();
                        cabec.versaoDados    = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                    #endregion
                }
                else
                {
                    switch (Acesso.xUFtoWS)
                    {
                    case "SP":
                    {
                        #region Regiao_SP
                        if (Acesso.TP_AMB == 1)
                        {
                            HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        else if (Acesso.TP_AMB == 2)
                        {
                            HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        #endregion
                    }
                    break;

                    case "SVRS":
                    {
                        #region Regiao_SVRS
                        if (Acesso.TP_AMB == 1)
                        {
                            HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        else if (Acesso.TP_AMB == 2)
                        {
                            HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        #endregion
                    }
                    break;

                    case "MG":
                    {
                        #region Regiao_SP
                        if (Acesso.TP_AMB == 1)
                        {
                            HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        else if (Acesso.TP_AMB == 2)
                        {
                            HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        #endregion
                    }
                    break;

                    case "MS":
                    {
                        #region Regiao_SP
                        if (Acesso.TP_AMB == 1)
                        {
                            HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        else if (Acesso.TP_AMB == 2)
                        {
                            //HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MS.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.NfeRetRecepcao2();
                            //HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg();
                            //cabec.cUF = Acesso.cUF.ToString();
                            //cabec.versaoDados = Acesso.versaoNFe;
                            //ws2.nfeCabecMsgValue = cabec;
                            //ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            //XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            //xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            //XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            //sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        #endregion
                    }
                    break;

                    case "RS":
                    {
                        #region Regiao_SP
                        if (Acesso.TP_AMB == 1)
                        {
                            HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        else if (Acesso.TP_AMB == 2)
                        {
                            HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.NfeRetRecepcao2 ws2   = new HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.NfeRetRecepcao2();
                            HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.nfeCabecMsg     cabec = new HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.nfeCabecMsg();
                            cabec.cUF            = Acesso.cUF.ToString();
                            cabec.versaoDados    = Acesso.versaoNFe;
                            ws2.nfeCabecMsgValue = cabec;
                            ws2.ClientCertificates.Add(Acesso.cert_NFe);
                            XmlDocument xmlNfeDadosMsg = new XmlDocument();
                            xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                            XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                            sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                        }
                        #endregion
                    }
                    break;
                    }
                    //fazer Ret Recepcao para MS E RS
                }
                XmlDocument xmlRet = new XmlDocument();
                xmlRet.LoadXml(sXmlRetorno);
                return(xmlRet);
            }
            catch (Exception x)
            {
                throw x;
            }
        }
Beispiel #21
0
        private void CarregaItensEventoCartaCorrecao(belPesquisaCCe objbelPesquisa)
        {
            try
            {
                if (Acesso.NM_RAMO != Acesso.BancoDados.TRANSPORTE)
                {
                    objEnvEvento = new belEnvEvento();
                    objEnvEvento.versao = this._VERSAO;
                    objEnvEvento.id = objbelPesquisa.CD_NRLANC;
                    belEvento objEvento = new belEvento();
                    objEvento.versao = _VERSAO;
                    objEvento.infEvento = new _infEvento();
                    objEvento.infEvento.CNPJ = objbelPesquisa.CNPJ;
                    objEvento.infEvento.CPF = objbelPesquisa.CPF;
                    objEvento.infEvento.dhEvento = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss" + Acesso.FUSO);
                    objEvento.infEvento.verEvento = _VERSAO;
                    objEvento.infEvento.tpAmb = Acesso.TP_AMB;
                    objEvento.infEvento.chNFe = objbelPesquisa.CHNFE;
                    objEvento.infEvento.cOrgao = Acesso.cUF.ToString();
                    objEvento.infEvento.detEvento = new _detEvento
                    {
                        versao = _VERSAO
                    };
                    objEvento.infEvento.detEvento.xCorrecao = BuscaCorrecoes(objbelPesquisa.CD_NRLANC);
                    objEvento.infEvento.nSeqEvento = objbelPesquisa.QT_ENVIO + 1;
                    objEvento.idLote = "ID" + objEvento.infEvento.tpEvento + objEvento.infEvento.chNFe + objEvento.infEvento.nSeqEvento.ToString().PadLeft(2, '0');
                    objEnvEvento.evento.Add(objEvento);
                }
                else
                {

                    objEvCCeCTe = new TEvento();

                    objEvCCeCTe.versao = Acesso.versaoCTe;
                    objEvCCeCTe.infEvento = new  TEventoInfEvento();
                    objEvCCeCTe.infEvento.tpEvento = "110110";
                    objEvCCeCTe.infEvento.nSeqEvento = (objbelPesquisa.QT_ENVIO + 1).ToString();// numero de evento
                    objEvCCeCTe.infEvento.Id = "ID" + objEvCCeCTe.infEvento.tpEvento + objlItensPesquisa.FirstOrDefault().CHNFE + objEvCCeCTe.infEvento.nSeqEvento.ToString().PadLeft(2, '0');
                    //objEvCCeCTe.infEvento.Id = "ID" + objlItensPesquisa.FirstOrDefault().CHNFE;                   
                    objEvCCeCTe.infEvento.cOrgao = Convert.ToByte(Acesso.cUF);
                    objEvCCeCTe.infEvento.tpAmb = Acesso.TP_AMB == 1 ? TAmb.Item1 : TAmb.Item2;
                    objEvCCeCTe.infEvento.CNPJ = Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA);
                    objEvCCeCTe.infEvento.chCTe = objbelPesquisa.CHNFE;
                    objEvCCeCTe.infEvento.dhEvento = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss");
                    objEvCCeCTe.infEvento.detEvento = new TEventoInfEventoDetEvento();
                    objEvCCeCTe.infEvento.detEvento.versaoEvento = Acesso.versaoCTe;

                    evCCeCTe objEvCanc = new evCCeCTe();
                    objEvCanc.descEvento = evCCeCTeDescEvento.CartadeCorrecao;
                    objEvCanc.infCorrecao = new List<evCCeCTeInfCorrecao>();
                    objEvCanc.CD_NRLANC = objbelPesquisa.CD_NRLANC;
                    foreach (DataRow item in BuscaCorrecoesCTe(objbelPesquisa.CD_NRLANC).Rows)
                    {
                        objEvCanc.infCorrecao.Add(new evCCeCTeInfCorrecao
                        {
                            grupoAlterado = item["grupoAlterado"].ToString(),
                            campoAlterado = item["campoAlterado"].ToString(),
                            valorAlterado = item["valorAlterado"].ToString(),
                            nroItemAlterado = item["nroItemAlterado"].ToString(),
                        });
                    }
                    objEvCanc.xCondUso = evCCeCTeXCondUso.ACartadeCorreçãoédisciplinadapeloArt58BdoCONVÊNIOSINIEF0689FicapermitidaautilizaçãodecartadecorreçãopararegularizaçãodeerroocorridonaemissãodedocumentosfiscaisrelativosàprestaçãodeserviçodetransportedesdequeoerronãoestejarelacionadocomIasvariáveisquedeterminamovalordoimpostotaiscomobasedecálculoalíquotadiferençadepreçoquantidadevalordaprestaçãoIIacorreçãodedadoscadastraisqueimpliquemudançadoemitentetomadorremetenteoudodestinatárioIIIadatadeemissãooudesaída;
                    string xFile = Pastas.CCe + "\\" + objEvCanc.CD_NRLANC + "_prevalida.xml";
                    SerializeClassToXml.SerializeClasse<evCCeCTe>(objEvCanc, xFile);
                    XmlDocument xml = new XmlDocument();
                    xml.Load(xFile);
                    objEvCCeCTe.infEvento.detEvento.Any = (System.Xml.XmlElement)xml.DocumentElement;


                    XmlSerializer xs = new XmlSerializer(typeof(TEvento));
                    MemoryStream memory = new MemoryStream();
                    XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);
                    xs.Serialize(xmltext, objEvCCeCTe);
                    UTF8Encoding encoding = new UTF8Encoding();
                    this.sXMLfinal = encoding.GetString(memory.ToArray());
                    this.sXMLfinal = this.sXMLfinal.Substring(1);

                    belAssinaXml Assinatura = new belAssinaXml();
                    this.sXMLfinal = Assinatura.ConfigurarArquivo(this.sXMLfinal, "infEvento", Acesso.cert_CTe);

                    xml = new XmlDocument();
                    xml.LoadXml(this.sXMLfinal);
                    string sPath = Pastas.PROTOCOLOS + "\\" + objbelPesquisa.CHNFE + "_ped-cce.xml";
                    if (File.Exists(sPath))
                    {
                        File.Delete(sPath);
                    }
                    xml.Save(sPath);
                    try
                    {
                        belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/cte", Pastas.SCHEMA_CTE + "\\eventoCTe_v2.00.xsd", sPath);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        private XmlDocument Exec_Cosulta(string sRec)
        {
            try
            {
                string snfeDadosMsg = NfeDadosMsg(sRec);

                belAssinaXml Assina = new belAssinaXml();
                string sRet = string.Empty;
                string sXmlRetorno = string.Empty;

                if (Acesso.TP_EMIS == 6)
                {
                    if (Acesso.TP_AMB == 1)
                    {
                        HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_SVC_Producao_NfeRetRecepcao.nfeCabecMsg();
                        cabec.cUF = Acesso.cUF.ToString();
                        cabec.versaoDados = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                    else
                    {
                        HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_SVC_Homologacao_NfeRetRecepcao.nfeCabecMsg();
                        cabec.cUF = Acesso.cUF.ToString();
                        cabec.versaoDados = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                }
                else if (Acesso.TP_EMIS == 3)
                {
                    #region SCAN
                    if (Acesso.TP_AMB == 1)
                    {
                        HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_SCAN_Producao_NFeRetRecepcao.nfeCabecMsg();
                        cabec.cUF = Acesso.cUF.ToString();
                        cabec.versaoDados = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                    else if (Acesso.TP_AMB == 2)
                    {
                        HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.NfeRetRecepcao2();
                        HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_SCAN_Homologacao_NFeRetRecepcao.nfeCabecMsg();
                        cabec.cUF = Acesso.cUF.ToString();
                        cabec.versaoDados = Acesso.versaoNFe;
                        ws2.nfeCabecMsgValue = cabec;
                        ws2.ClientCertificates.Add(Acesso.cert_NFe);
                        XmlDocument xmlNfeDadosMsg = new XmlDocument();
                        xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                        XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                        sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                    }
                    #endregion
                }
                else
                {
                    switch (Acesso.xUFtoWS)
                    {
                        case "SP":
                            {
                                #region Regiao_SP
                                if (Acesso.TP_AMB == 1)
                                {
                                    HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_SP.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                else if (Acesso.TP_AMB == 2)
                                {
                                    HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                #endregion
                            }
                            break;
                        case "SVRS":
                            {
                                #region Regiao_SVRS
                                if (Acesso.TP_AMB == 1)
                                {
                                    HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.nfeCabecMsg cabec = new HLP.GeraXml.WebService.V2_Producao_RetRecepcao_SVRS.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                else if (Acesso.TP_AMB == 2)
                                {
                                    HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.nfeCabecMsg cabec = new HLP.GeraXml.WebService.V2_Homologacao_RetRecepcao_SVRS.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                #endregion
                            }
                            break;
                        case "MG":
                            {
                                #region Regiao_SP
                                if (Acesso.TP_AMB == 1)
                                {
                                    HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Producao_NfeRetRecepcao_MG.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                else if (Acesso.TP_AMB == 2)
                                {
                                    HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MG.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                #endregion
                            }
                            break;
                        case "MS":
                            {
                                #region Regiao_SP
                                if (Acesso.TP_AMB == 1)
                                {
                                    HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_MS.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                else if (Acesso.TP_AMB == 2)
                                {
                                    //HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_MS.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.NfeRetRecepcao2();
                                    //HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Homologacao_NfeRetRecepcao_SP.nfeCabecMsg();
                                    //cabec.cUF = Acesso.cUF.ToString();
                                    //cabec.versaoDados = Acesso.versaoNFe;
                                    //ws2.nfeCabecMsgValue = cabec;
                                    //ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    //XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    //xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    //XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    //sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                #endregion
                            }
                            break;
                        case "RS":
                            {
                                #region Regiao_SP
                                if (Acesso.TP_AMB == 1)
                                {
                                    HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Producao_NFeRetRecepcao_RS.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                else if (Acesso.TP_AMB == 2)
                                {
                                    HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.NfeRetRecepcao2 ws2 = new HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.NfeRetRecepcao2();
                                    HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.nfeCabecMsg cabec = new HLP.GeraXml.WebService.v2_Homologacao_NFeRetRecepacao_RS.nfeCabecMsg();
                                    cabec.cUF = Acesso.cUF.ToString();
                                    cabec.versaoDados = Acesso.versaoNFe;
                                    ws2.nfeCabecMsgValue = cabec;
                                    ws2.ClientCertificates.Add(Acesso.cert_NFe);
                                    XmlDocument xmlNfeDadosMsg = new XmlDocument();
                                    xmlNfeDadosMsg.LoadXml(snfeDadosMsg);
                                    XmlNode xNodeRet = xmlNfeDadosMsg.DocumentElement;
                                    sXmlRetorno = ws2.nfeRetRecepcao2(xNodeRet).OuterXml;
                                }
                                #endregion
                            }
                            break;


                    }
                    //fazer Ret Recepcao para MS E RS
                }
                XmlDocument xmlRet = new XmlDocument();
                xmlRet.LoadXml(sXmlRetorno);
                return xmlRet;
            }
            catch (Exception x)
            {
                throw x;
            }
        }
Beispiel #23
0
        public List<belStatusCte> GerarXmlInutilizacao(belInutilizaFaixaCte objInutiliza)
        {
            XNamespace pf = "http://www.portalfiscal.inf.br/cte";
            XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";

            XContainer inutCTe = new XElement(pf + "inutCTe", new XAttribute("versao", Acesso.versaoCTe),
                                                                 new XAttribute(ns + "schemaLocation", "http://www.portalfiscal.inf.br/inutCte_v1.04.xsd"),
                                                                 new XAttribute("xmlns", "http://www.portalfiscal.inf.br/cte"),
                                                                 new XAttribute(XNamespace.Xmlns + "ds", "http://www.w3.org/2000/09/xmldsig#"),
                                                                 new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));

            XContainer infInut = new XElement(pf + "infInut", new XAttribute("Id", objInutiliza.Id),
                                                                 new XElement(pf + "tpAmb", objInutiliza.tpAmb),
                                                                 new XElement(pf + "xServ", objInutiliza.xServ),
                                                                 new XElement(pf + "cUF", objInutiliza.cUF),
                                                                 new XElement(pf + "ano", objInutiliza.ano),
                                                                 new XElement(pf + "CNPJ", objInutiliza.CNPJ),
                                                                 new XElement(pf + "mod", objInutiliza.mod),
                                                                 new XElement(pf + "serie", objInutiliza.serie),
                                                                 new XElement(pf + "nCTIni", objInutiliza.nCTIni),
                                                                 new XElement(pf + "nCTFin", objInutiliza.nCTFin),
                                                                 new XElement(pf + "xJust", objInutiliza.xJust));


            inutCTe.Add(infInut);

            #region Assinatura

            belAssinaXml Assinatura = new belAssinaXml();
            string sInutCte = Assinatura.ConfigurarArquivo(inutCTe.ToString(), "infInut", Acesso.cert_CTe);
            XElement xInutCTe = XElement.Parse(sInutCte);


            #endregion

            #region Salva Xml

            string sData = daoUtil.GetDateServidor().Date.ToString("dd-MM-yyyy");
            DirectoryInfo dPastaProtocolo = new DirectoryInfo(Pastas.PROTOCOLOS);
            if (!dPastaProtocolo.Exists) { dPastaProtocolo.Create(); }

            DirectoryInfo dPastaMesAtual = new DirectoryInfo(dPastaProtocolo + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2));
            if (!dPastaMesAtual.Exists) { dPastaMesAtual.Create(); }

            string sCaminho = dPastaMesAtual.ToString() + "\\" + "Inut_" + objInutiliza.nCTIni + "_" + objInutiliza.nCTFin + ".xml";

            XDocument XmlInut = new XDocument(xInutCTe);
            XmlInut.Save(sCaminho);

            #endregion

            #region Valida_Xml


            belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/cte", Pastas.SCHEMA_CTE + "\\inutCte_v1.04.xsd", sCaminho);



            #endregion

            #region Inutilizar Cte


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

            string sRetorno = InutilizaFaixaCte(doc);
            List<belStatusCte> ListaStatus = TrataDadosRetorno(sRetorno);

            foreach (belStatusCte cte in ListaStatus)
            {
                if (cte.CodRetorno == "102")
                {
                    XDocument xRet = XDocument.Parse(sRetorno);
                    xRet.Save(sCaminho);
                }
                else
                {
                    File.Delete(sCaminho);
                }
            }
            #endregion


            return ListaStatus;

        }
Beispiel #24
0
        public void GeraXmlEnvio()
        {
            try
            {
                belAssinaXml Assinatura     = new belAssinaXml();
                XDocument    xdoc           = new XDocument();
                XNamespace   pf             = "http://www.portalfiscal.inf.br/nfe";
                XContainer   xContEnvEvento = new XElement(pf + "envEvento", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
                                                           new XAttribute("versao", objEnvEvento.versao),
                                                           new XElement(pf + "idLote", objEnvEvento.id));

                //List<string> ListaEventos = new List<string>();
                objXmlsSeparados = new Dictionary <string, string>();
                foreach (var obj in objEnvEvento.evento)
                {
                    XContainer xConEvento = new XElement(pf + "evento", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
                                                         new XAttribute("versao", obj.versao),
                                                         new XElement(pf + "infEvento", new XAttribute("Id", obj.idLote),
                                                                      new XElement(pf + "cOrgao", obj.infEvento.cOrgao),
                                                                      new XElement(pf + "tpAmb", obj.infEvento.tpAmb),
                                                                      new XElement(pf + (obj.infEvento.CNPJ != "" ? "CNPJ" : "CPF"), (obj.infEvento.CNPJ != "" ? obj.infEvento.CNPJ : obj.infEvento.CPF)),
                                                                      new XElement(pf + "chNFe", obj.infEvento.chNFe),
                                                                      new XElement(pf + "dhEvento", obj.infEvento.dhEvento), //-02:00
                                                                      new XElement(pf + "tpEvento", obj.infEvento.tpEvento),
                                                                      new XElement(pf + "nSeqEvento", obj.infEvento.nSeqEvento),
                                                                      new XElement(pf + "verEvento", obj.infEvento.verEvento),
                                                                      new XElement(pf + "detEvento", new XAttribute("versao", obj.versao),
                                                                                   new XElement(pf + "descEvento", obj.infEvento.detEvento.descEvento),
                                                                                   new XElement(pf + "xCorrecao", obj.infEvento.detEvento.xCorrecao),
                                                                                   new XElement(pf + "xCondUso", obj.infEvento.detEvento.xCondUso))));


                    objXmlsSeparados.Add(obj.infEvento.chNFe, Assinatura.ConfigurarArquivo(xConEvento.ToString(), "infEvento", Acesso.cert_NFe));
                }


                string sEventos = "";
                foreach (KeyValuePair <string, string> sInfEvento in objXmlsSeparados)
                {
                    sEventos += sInfEvento.Value;
                }

                sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envEvento xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"1.00\"><idLote>" + objEnvEvento.id.ToString()
                            + "</idLote>" + sEventos + "</envEvento>";

                StreamWriter sr = new StreamWriter(Pastas.CCe + "\\" + objEnvEvento.evento.FirstOrDefault().idLote + "_prevalida.xml");
                sr.Write(sXMLfinal);
                sr.Close();


                #region Valida_Xml


                XmlValidatingReader reader;
                try
                {
                    XmlSchemaCollection myschema = new XmlSchemaCollection();
                    myschema.Add("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CCE + "\\envCCe_v1.00.xsd");

                    XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
                    reader = new XmlValidatingReader(sXMLfinal, XmlNodeType.Element, context);
                    reader.ValidationType = ValidationType.Schema;
                    reader.Schemas.Add(myschema);
                    while (reader.Read())
                    {
                    }
                }
                catch (XmlException x)
                {
                    throw new Exception(x.Message);
                }
                catch (XmlSchemaException x)
                {
                    throw new Exception(x.Message);
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #25
0
        public void GerarArquivoXml2(tcLoteRps objLoteRpd)
        {
            try
            {
                List<string> ListaNfes = new List<string>();
                XDocument xdoc = new XDocument();
                XNamespace pf = "http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd";


                XContainer conEnviarLoteRpsEnvio = (new XElement(pf + "EnviarLoteRpsEnvio", new XAttribute("xmlns", "http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd")));

                XContainer conLoteRps = null;
                conLoteRps = (new XElement(pf + "LoteRps", new XAttribute("Id", Convert.ToInt32(objLoteRpd.NumeroLote)),
                                                                                     new XElement(pf + "NumeroLote", objLoteRpd.NumeroLote.ToString()),
                                                                                     new XElement(pf + "Cnpj", objLoteRpd.Cnpj.ToString()),
                                                                                     new XElement(pf + "InscricaoMunicipal", objLoteRpd.InscricaoMunicipal.ToString()),
                                                                                     new XElement(pf + "QuantidadeRps", objLoteRpd.QuantidadeRps.ToString())));

                XContainer conListaRps = (new XElement(pf + "ListaRps"));
                XContainer conRps = (new XElement(pf + "Rps"));
                XContainer conInfRps = null;
                XContainer conSubstituto = null;
                XContainer conServico = null;
                XContainer conPrestador = null;
                XContainer conTomador = null;
                XContainer conIntermediarioServico = null;
                XContainer conConstrucaoCivil = null;


                belAssinaXml Assinatura = new belAssinaXml();

                foreach (TcRps rps in objLoteRpd.Rps)
                {
                    #region IdentificacaoRps conRps
                    try
                    {
                        conInfRps = (new XElement(pf + "InfRps", new XElement(pf + "IdentificacaoRps",
                                                                                     new XElement(pf + "Numero", rps.InfRps.IdentificacaoRps.Numero),
                                                                                     new XElement(pf + "Serie", rps.InfRps.IdentificacaoRps.Serie),
                                                                                     new XElement(pf + "Tipo", rps.InfRps.IdentificacaoRps.Tipo)),
                                                                          new XElement(pf + "DataEmissao", daoUtil.GetDateServidor().Date.ToString("yyyy-MM-ddTHH:mm:ss")),
                                                                          new XElement(pf + "NaturezaOperacao", rps.InfRps.NaturezaOperacao),
                                                                         ((rps.InfRps.RegimeEspecialTributacao != 0) ? new XElement(pf + "RegimeEspecialTributacao", rps.InfRps.RegimeEspecialTributacao) : null),
                                                                          new XElement(pf + "OptanteSimplesNacional", rps.InfRps.OptanteSimplesNacional),
                                                                          new XElement(pf + "IncentivadorCultural", rps.InfRps.IncentivadorCultural),
                                                                          new XElement(pf + "Status", rps.InfRps.Status)));

                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('IdentificacaoRps')- "
                            + Environment.NewLine + "XML_Detalhes: "
                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region RpsSubstituido
                    if (rps.InfRps.RpsSubstituido != null)
                    {
                        try
                        {
                            conSubstituto = (new XElement("RpsSubstituido",
                                                         new XElement("Numero", rps.InfRps.RpsSubstituido.Numero),
                                                         new XElement("Serie", rps.InfRps.RpsSubstituido.Serie),
                                                         new XElement("Tipo", rps.InfRps.RpsSubstituido.Tipo)));
                        }
                        catch (Exception x)
                        {
                            throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('RpsSubstituido')- "
                                + Environment.NewLine + "XML_Detalhes: "
                                + Environment.NewLine + x.Message);
                        }
                    }

                    #endregion

                    #region Servico
                    try
                    {
                        conServico = (new XElement(pf + "Servico", new XElement(pf + "Valores",
                                                    new XElement(pf + "ValorServicos", rps.InfRps.Servico.Valores.ValorServicos.ToString("#0.00").Replace(",", ".")),
                                                    ((rps.InfRps.Servico.Valores.ValorDeducoes != 0) ? new XElement(pf + "ValorDeducoes", rps.InfRps.Servico.Valores.ValorDeducoes.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.ValorPis != null) ? new XElement(pf + "ValorPis", rps.InfRps.Servico.Valores.ValorPis.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.ValorCofins != null) ? new XElement(pf + "ValorCofins", rps.InfRps.Servico.Valores.ValorCofins.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.ValorInss != null) ? new XElement(pf + "ValorInss", rps.InfRps.Servico.Valores.ValorInss.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.ValorIr != null) ? new XElement(pf + "ValorIr", rps.InfRps.Servico.Valores.ValorIr.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.ValorCsll != null) ? new XElement(pf + "ValorCsll", rps.InfRps.Servico.Valores.ValorCsll.ToString("#0.00").Replace(",", ".")) : null),
                                                                                                      new XElement(pf + "IssRetido", rps.InfRps.Servico.Valores.IssRetido),
                                                    ((rps.InfRps.Servico.Valores.ValorIss != null) ? new XElement(pf + "ValorIss", rps.InfRps.Servico.Valores.ValorIss.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.ValorIssRetido != null) ? new XElement(pf + "ValorIssRetido", rps.InfRps.Servico.Valores.ValorIssRetido.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.OutrasRetencoes != 0) ? new XElement(pf + "OutrasRetencoes", rps.InfRps.Servico.Valores.OutrasRetencoes.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.BaseCalculo != 0) ? new XElement(pf + "BaseCalculo", rps.InfRps.Servico.Valores.BaseCalculo.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.Aliquota != null) ? new XElement(pf + "Aliquota", (rps.InfRps.Servico.Valores.Aliquota / 100).ToString("#0.0000").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.ValorLiquidoNfse != 0) ? new XElement(pf + "ValorLiquidoNfse", rps.InfRps.Servico.Valores.ValorLiquidoNfse.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.DescontoCondicionado != 0) ? new XElement(pf + "DescontoCondicionado", rps.InfRps.Servico.Valores.DescontoCondicionado.ToString("#0.00").Replace(",", ".")) : null),
                                                    ((rps.InfRps.Servico.Valores.DescontoIncondicionado != 0) ? new XElement(pf + "DescontoIncondicionado", rps.InfRps.Servico.Valores.DescontoIncondicionado.ToString("#0.00").Replace(",", ".")) : null)),
                                                                                         new XElement(pf + "ItemListaServico", rps.InfRps.Servico.ItemListaServico),
                                       ((rps.InfRps.Servico.CodigoCnae != "") ? new XElement(pf + "CodigoCnae", rps.InfRps.Servico.CodigoCnae) : null),
                                       (((rps.InfRps.Servico.CodigoTributacaoMunicipio != "") && (rps.InfRps.Servico.CodigoTributacaoMunicipio != "0")) ? new XElement(pf + "CodigoTributacaoMunicipio", rps.InfRps.Servico.CodigoTributacaoMunicipio) : null),
                                                                                         new XElement(pf + "Discriminacao", rps.InfRps.Servico.Discriminacao),
                                       new XElement(pf + "CodigoMunicipio", rps.InfRps.Servico.CodigoMunicipio)));
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('Servico')- "
                            + Environment.NewLine + "XML_Detalhes: "
                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region Prestador
                    try
                    {
                        conPrestador = (new XElement(pf + "Prestador", new XElement(pf + "Cnpj", rps.InfRps.Prestador.Cnpj),
                                                                      ((rps.InfRps.Prestador.InscricaoMunicipal != "") ? new XElement(pf + "InscricaoMunicipal", rps.InfRps.Prestador.InscricaoMunicipal) : null)));

                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('Prestador')- "
                            + Environment.NewLine + "XML_Detalhes: "
                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region Tomador
                    try
                    {
                        //conTomador = (new XElement(pf + "Tomador", ((rps.InfRps.Tomador.IdentificacaoTomador != null) ? new XElement(pf + "IdentificacaoTomador", new XElement(pf + "CpfCnpj",
                        //                                                                                       new XElement(pf + (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? "Cnpj" : "Cpf"), (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj : rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cpf))),
                        //                                            (rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal != "" ? (new XElement(pf + "InscricaoMunicipal", rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal)) : null)) : null),
                        //                                            new XElement(pf + "RazaoSocial", rps.InfRps.Tomador.RazaoSocial),
                        //                                            new XElement(pf + "Endereco",
                        //                                                               new XElement(pf + "Endereco", rps.InfRps.Tomador.Endereco.Endereco),
                        //                                                               new XElement(pf + "Numero", rps.InfRps.Tomador.Endereco.Numero),
                        //                                                              (rps.InfRps.Tomador.Endereco.Complemento != "" ? new XElement(pf + "Complemento", rps.InfRps.Tomador.Endereco.Complemento) : null),
                        //                                                               new XElement(pf + "Bairro", rps.InfRps.Tomador.Endereco.Bairro),
                        //                                                               new XElement(pf + "CodigoMunicipio", rps.InfRps.Tomador.Endereco.CodigoMunicipio),
                        //                                                               new XElement(pf + "Uf", rps.InfRps.Tomador.Endereco.Uf),
                        //                                                               new XElement(pf + "Cep", rps.InfRps.Tomador.Endereco.Cep))));

                        conTomador = (new XElement(pf + "Tomador", ((rps.InfRps.Tomador.IdentificacaoTomador != null) ? new XElement(pf + "IdentificacaoTomador", new XElement(pf + "CpfCnpj",
                                                                                                              new XElement(pf + (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? "Cnpj" : "Cpf"), (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj : rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cpf))),
                                                                   (rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal != "" ? (new XElement(pf + "InscricaoMunicipal", rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal)) : null)) : null),
                                                                   new XElement(pf + "RazaoSocial", rps.InfRps.Tomador.RazaoSocial),
                                                                   new XElement(pf + "Endereco",
                                                                                      new XElement(pf + "Endereco", rps.InfRps.Tomador.Endereco.Endereco),
                                                                                      new XElement(pf + "Numero", rps.InfRps.Tomador.Endereco.Numero),
                                                                                     (rps.InfRps.Tomador.Endereco.Complemento != "" ? new XElement(pf + "Complemento", rps.InfRps.Tomador.Endereco.Complemento) : null),
                                                                                      new XElement(pf + "Bairro", rps.InfRps.Tomador.Endereco.Bairro),
                                                                                      new XElement(pf + "CodigoMunicipio", rps.InfRps.Tomador.Endereco.CodigoMunicipio),
                                                                                      new XElement(pf + "Uf", rps.InfRps.Tomador.Endereco.Uf),
                                                                                      new XElement(pf + "Cep", rps.InfRps.Tomador.Endereco.Cep)),
                                                                                      ((rps.InfRps.Tomador.Contato.Telefone != "" || rps.InfRps.Tomador.Contato.Email != "") ?
                                                                                      (new XElement(pf + "Contato",
                                                                                                        ((rps.InfRps.Tomador.Contato.Telefone != "") ? new XElement(pf + "Telefone", rps.InfRps.Tomador.Contato.Telefone) : null),
                                                                                                        ((rps.InfRps.Tomador.Contato.Email != "") ? new XElement(pf + "Email", rps.InfRps.Tomador.Contato.Email) : null))) : null)));



                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('Tomador')- "
                            + Environment.NewLine + "XML_Detalhes: "
                            + Environment.NewLine + x.Message);
                    }

                    #endregion

                    #region IntermediarioServico
                    try
                    {
                        if (rps.InfRps.IntermediarioServico != null)
                        {
                            conIntermediarioServico = (new XElement(pf + "IntermediarioServico", new XElement(pf + "RazaoSocial", rps.InfRps.IntermediarioServico.RazaoSocial),
                                                                                               new XElement(pf + "CpfCnpj", (new XElement((rps.InfRps.IntermediarioServico.CpfCnpj.Cnpj != null ? "Cnpj" : "Cpf"), (rps.InfRps.IntermediarioServico.CpfCnpj.Cnpj != null ? rps.InfRps.IntermediarioServico.CpfCnpj.Cnpj : rps.InfRps.IntermediarioServico.CpfCnpj.Cpf)))),
                                                                                               new XElement(pf + "InscricaoMunicipal", rps.InfRps.IntermediarioServico.InscricaoMunicipal)));
                        }
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('IntermediarioServico')- "
                            + Environment.NewLine + "XML_Detalhes: "
                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region ConstrucaoCivil

                    try
                    {
                        if (rps.InfRps.ConstrucaoCivil != null)
                        {
                            if ((rps.InfRps.ConstrucaoCivil.CodigoObra != "") && (rps.InfRps.ConstrucaoCivil.Art != ""))
                            {
                                conConstrucaoCivil = (new XElement(pf + "ConstrucaoCivil", new XElement(pf + "CodigoObra", rps.InfRps.ConstrucaoCivil.CodigoObra),
                                                                                          new XElement(pf + "Art", rps.InfRps.ConstrucaoCivil.Art)));
                            }
                        }

                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('ConstrucaoCivil')- "
                            + Environment.NewLine + "XML_Detalhes: "
                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    try
                    {
                        if (conSubstituto != null)
                        {
                            conInfRps.Add(conSubstituto);
                        }
                        conInfRps.Add(conServico);
                        conInfRps.Add(conPrestador);
                        conInfRps.Add(conTomador);
                        if (conIntermediarioServico != null) { conInfRps.Add(conIntermediarioServico); }
                        if (conConstrucaoCivil != null) { conInfRps.Add(conConstrucaoCivil); }
                        conRps.Add(conInfRps);


                        conListaRps.Add(conRps);


                        //Salva o Rps;
                        XDocument xdocsalvanfesemlot = new XDocument(conRps);
                        string sPasta = rps.InfRps.DataEmissao.ToString("MM/yy").Replace("/", "");
                        string sNomeArquivo = rps.InfRps.DataEmissao.ToString("MM/yy").Replace("/", "") + rps.InfRps.IdentificacaoRps.Serie + rps.InfRps.IdentificacaoRps.Numero;
                        DirectoryInfo dPastaData = new DirectoryInfo(Pastas.ENVIO + "\\Servicos\\" + sPasta);
                        if (!dPastaData.Exists) { dPastaData.Create(); }
                        xdocsalvanfesemlot.Save(Pastas.ENVIO + "\\Servicos\\" + sPasta + "\\" + sNomeArquivo + "-nfes.xml");

                        conRps = (new XElement("Rps"));
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + "sNota" + "Erro ao assinar a nfe de sequencia " + "sNota" + x.Message);
                    }
                }

                conLoteRps.Add(conListaRps);
                conEnviarLoteRpsEnvio.Add(conLoteRps);
                //Assina Arquivo
                string nfes = Assinatura.ConfigurarArquivo(conEnviarLoteRpsEnvio.ToString(), "LoteRps", Acesso.cert_NFs);
                sXmlLote = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + nfes;
                //Grava
                string sPathLote = Pastas.ENVIO + "\\Servicos\\" + "Lote_" + objLoteRpd.NumeroLote + ".xml";
                StreamWriter sw = new StreamWriter(sPathLote);
                sw.Write(sXmlLote);
                sw.Close();

                #region Valida_Xml


                try
                {
                    if (Acesso.tipoWsNfse == Acesso.TP_WS_NFSE.GINFES)
                    {
                        belValidaXml.ValidarXml("http://www.ginfes.com.br/servico_enviar_lote_rps_envio_v03.xsd", Pastas.SCHEMA_NFSE + "\\servico_enviar_lote_rps_envio_v03.xsd", sPathLote);
                    }
                    else
                    {
                        belValidaXml.ValidarXml("http://www.nfe.com.br/WSNacional/XSD/1/nfse_municipal_v01.xsd", Pastas.SCHEMA_NFSE + "\\TIPLAN\\nfse_municipal_v01.xsd", sPathLote);
                    }
                }
                catch (XmlException x)
                {
                    File.Delete(sPathLote);
                    throw new Exception(x.Message);
                }
                catch (XmlSchemaException x)
                {
                    File.Delete(sPathLote);
                    throw new Exception(x.Message);
                }
                #endregion

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #26
0
        public bool ExecuteEvento()
        {
            try
            {
                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                nameSpaces.Add("", "");
                nameSpaces.Add("", "http://www.portalfiscal.inf.br/mdfe");

                XmlSerializer xs      = new XmlSerializer(typeof(TEvento));
                MemoryStream  memory  = new MemoryStream();
                XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);
                xs.Serialize(xmltext, evento, nameSpaces);
                UTF8Encoding encoding = new UTF8Encoding();
                string       sEvento  = encoding.GetString(memory.ToArray());
                sEvento = sEvento.Substring(1);


                belAssinaXml Assinatura = new belAssinaXml();
                sEvento = Assinatura.ConfigurarArquivo(sEvento, "infEvento", Acesso.cert_CTe);

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(sEvento);
                string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.chaveMDFe + "_ped-can-mdfe.xml";
                if (File.Exists(sPath))
                {
                    File.Delete(sPath);
                }
                xDoc.Save(sPath);
                try
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/mdfe", Pastas.SCHEMA_MDFe + "\\eventoMDFe_v1.00.xsd", sPath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                #region Cancelar Cte
                XmlDocument doc = new XmlDocument();
                doc.Load(sPath);
                string    sRetorno = Execute(doc);
                XDocument xRet     = XDocument.Parse(sRetorno);
                xRet.Save(sPath);
                retorno = SerializeClassToXml.DeserializeClasse <TRetEvento>(sPath);
                if (retorno.infEvento.cStat != "135")
                {
                    File.Delete(sPath);
                }
                sMessage = string.Format("Codigo do Retorno: {0}{1}Motivo: {2}{1}Chave: {3}{1}Protocolo: {4}{1}",
                                         retorno.infEvento.cStat,
                                         Environment.NewLine,
                                         retorno.infEvento.xMotivo,
                                         retorno.infEvento.chMDFe,
                                         retorno.infEvento.nProt);

                if (retorno.infEvento.cStat == "135")
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #27
0
        public void GeraLoteXmlEnvio()
        {
            try
            {
                string sNomeArquivo = daoUtil.RetornaNomeArqNFe();
                this.sPathLote = (Acesso.TP_EMIS == 2 ? Pastas.CONTINGENCIA + sNomeArquivo : Pastas.ENVIO + sNomeArquivo);
                List<string> nfes = new List<string>();
                belIde objide;
                int iCount = 0;

                if (File.Exists(sPathLote))
                {
                    File.Delete(sPathLote);
                }
                string sCasasVlUnit = (Acesso.QTDE_CASAS_VL_UNIT == "" ? "000000" : ("").PadLeft(Convert.ToInt32(Acesso.QTDE_CASAS_VL_UNIT), '0'));

                foreach (belInfNFe nota in lNotas)
                {
                    string sUF;
                    XDocument xdoc = new XDocument();


                    #region XML_Cabeçalho
                    XNamespace pf = "http://www.portalfiscal.inf.br/nfe";
                    XContainer concabec = (new XElement(pf + "NFe", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe")));
                    XContainer coninfnfe = (new XElement(pf + "infNFe", new XAttribute("Id", nota.chaveNFe),
                                                                        new XAttribute("versao", "2.00")));
                    #endregion

                    #region Ide
                    XContainer conide;
                    try
                    {
                        objide = nota.ide;
                        #region XML_ide
                        conide = (new XElement(pf + "ide", new XElement(pf + "cUF", objide.Cuf.ToString()),
                                                                    new XElement(pf + "cNF", objide.Cnf.ToString()),
                                                                    new XElement(pf + "natOp", objide.Natop.ToString()),
                                                                    new XElement(pf + "indPag", objide.Indpag.ToString()),
                                                                    new XElement(pf + "mod", objide.Mod.ToString()),
                                                                    new XElement(pf + "serie", objide.Serie.ToString()),
                                                                    new XElement(pf + "nNF", objide.Nnf.ToString()),
                                                                    new XElement(pf + "dEmi", objide.Demi.ToString("yyyy-MM-dd")),
                                                                    new XElement(pf + "dSaiEnt", objide.Dsaient.ToString("yyyy-MM-dd")),
                                                                    new XElement(pf + "hSaiEnt", daoUtil.GetDateServidor().ToString("HH:mm:ss")), // NFe_2.0
                                                                    new XElement(pf + "tpNF", objide.Tpnf.ToString()),
                                                                    new XElement(pf + "cMunFG", objide.Cmunfg.ToString()),
                                                                    (objide.belNFref != null ?
                                                                    (from c in objide.belNFref
                                                                     select new XElement(pf + "NFref",
                                                                                 (!String.IsNullOrEmpty(c.RefNFe) ? new XElement(pf + "refNFe", c.RefNFe) : null),
                                                                                 (!String.IsNullOrEmpty(c.cUF) ? (new XElement(pf + "refNF",
                                                                                 (!String.IsNullOrEmpty(c.cUF) ? new XElement(pf + "cUF", c.cUF) : null),
                                                                                 (!String.IsNullOrEmpty(c.AAMM) ? new XElement(pf + "AAMM", c.AAMM) : null),
                                                                                 (!String.IsNullOrEmpty(c.CNPJ) ? new XElement(pf + "CNPJ", c.CNPJ) : null),
                                                                                 (!String.IsNullOrEmpty(c.mod) ? new XElement(pf + "mod", c.mod) : null),
                                                                                 (!String.IsNullOrEmpty(c.serie) ? new XElement(pf + "serie", c.serie) : null),
                                                                                 (!String.IsNullOrEmpty(c.nNF) ? new XElement(pf + "nNF", c.nNF) : null))) : null))) : null),//NFe_2.0_Verificar ID B14 - B20a - B20i - 
                                                                    new XElement(pf + "tpImp", objide.Tpimp.ToString()),
                                                                    new XElement(pf + "tpEmis", objide.Tpemis.ToString()),
                                                                    new XElement(pf + "cDV", objide.Cdv.ToString()),
                                                                    new XElement(pf + "tpAmb", objide.Tpamb.ToString()),
                                                                    new XElement(pf + "finNFe", objide.Finnfe.ToString()),
                                                                    new XElement(pf + "procEmi", objide.Procemi.ToString()),
                                                                    new XElement(pf + "verProc", objide.Verproc.ToString()),

                                                                    ((objide.Tpemis.Equals("2")) || (objide.Tpemis.Equals("3")) || (objide.Tpemis.Equals("6")) ? // os_svc
                                                                                          new XElement(pf + "dhCont", daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss")) : null), // NFe_2.0
                                                                    ((objide.Tpemis.Equals("2")) || (objide.Tpemis.Equals("3")) || (objide.Tpemis.Equals("6")) ?
                                                                                          new XElement(pf + "xJust", (Acesso.TP_EMIS == 2 ? "FALHA DE CONEXÃO COM INTERNET" : "FALHA COM WEB SERVICE DO ESTADO")) : null)));// NFe_2.0
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na geração do XML, Regiao XML_ide - " + x.Message);
                    }
                    #endregion

                    #region Emit

                    XContainer conemit;
                    try
                    {
                        belEmit objemit = nota.emit;
                        #region XML_Emit

                        conemit = (new XElement(pf + "emit", (new XElement(pf + "CNPJ", objemit.Cnpj.ToString())),
                                                  new XElement(pf + "xNome", objemit.Xnome.ToString()),
                                                  new XElement(pf + "xFant", objemit.Xfant.ToString()),
                                                          new XElement(pf + "enderEmit",
                                                                  new XElement(pf + "xLgr", objemit.Xlgr.ToString()),
                                                                  new XElement(pf + "nro", objemit.Nro.ToString()),
                                                                  (!String.IsNullOrEmpty(objemit.Xcpl) ? new XElement(pf + "xCpl", objemit.Xcpl.ToString()) : null),
                                                                  new XElement(pf + "xBairro", objemit.Xbairro.ToString()),
                                                                  new XElement(pf + "cMun", objemit.Cmun.ToString()),
                                                                  new XElement(pf + "xMun", objemit.Xmun.ToString()),
                                                                  new XElement(pf + "UF", objemit.Uf.ToString()),
                                                                  new XElement(pf + "CEP", objemit.Cep.ToString()),
                                                                  new XElement(pf + "cPais", objemit.Cpais.ToString()),
                                                                  new XElement(pf + "xPais", objemit.Xpais.ToString()),
                                                                  (!String.IsNullOrEmpty(objemit.Fone) ? new XElement(pf + "fone", objemit.Fone.ToString()) : null)),
                                                  (!String.IsNullOrEmpty(objemit.Ie) ? new XElement(pf + "IE", objemit.Ie.ToString()) : null),
                                                  (!String.IsNullOrEmpty(objemit.Iest) ? new XElement(pf + "IEST", objemit.Iest.ToString()) : null),
                                                  (!String.IsNullOrEmpty(objemit.Im) ? new XElement(pf + "IM", objemit.Im.ToString()) : null),
                                                  (!String.IsNullOrEmpty(objemit.Cnae) ? new XElement(pf + "CNAE", objemit.Cnae.ToString()) : null),
                                                  new XElement(pf + "CRT", objemit.CRT.ToString()))); // NFe_2.0

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na geração do XML, Regiao XML_Emit - " + x.Message);
                    }
                    #endregion

                    #region Dest
                    XContainer condest;
                    try
                    {

                        belDest objdest = nota.dest;
                        sUF = objdest.Uf.ToString();
                        #region XML_Dest
                        objdest.Ie = (objdest.Ie == null ? "" : objdest.Ie);


                        condest = (new XElement(pf + "dest",
                                                  (objdest.Cnpj == "EXTERIOR" ? new XElement(pf + "CNPJ") :
                                                     (!String.IsNullOrEmpty(objdest.Cnpj) ? new XElement(pf + "CNPJ", objdest.Cnpj) :
                                                                            new XElement(pf + "CPF", objdest.Cpf))),
                                                  new XElement(pf + "xNome", (Acesso.TP_EMIS == 2 ? "NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL" : objdest.Xnome.ToString().Trim())),
                                                  new XElement(pf + "enderDest",
                                                      new XElement(pf + "xLgr", objdest.Xlgr.ToString()),
                                                      new XElement(pf + "nro", (!String.IsNullOrEmpty(objdest.Nro) ? objdest.Nro.ToString() : "0")),
                                                      (objdest.Xcpl != "" ? new XElement(pf + "xCpl", objdest.Xcpl.ToString()) : null),
                                                      new XElement(pf + "xBairro", objdest.Xbairro.ToString()),
                                                      new XElement(pf + "cMun", objdest.Cmun.ToString()),
                                                      new XElement(pf + "xMun", objdest.Xmun.ToString()),
                                                      new XElement(pf + "UF", objdest.Uf.ToString()),
                                                      (!String.IsNullOrEmpty(objdest.Cep) ? new XElement(pf + "CEP", objdest.Cep.ToString()) : null),
                                                      new XElement(pf + "cPais", objdest.Cpais.ToString()),
                                                      (!String.IsNullOrEmpty(objdest.Xpais) ? new XElement(pf + "xPais", objdest.Xpais.ToString()) : null),
                                                      (!String.IsNullOrEmpty(objdest.Fone) ? new XElement(pf + "fone", objdest.Fone.ToString()) : null)),
                                                      (!String.IsNullOrEmpty(objdest.Ie) ? (objdest.Ie != "EXTERIOR" ? new XElement(pf + "IE", objdest.Ie.ToString().Trim()) : new XElement(pf + "IE")) : null),
                                                      (!String.IsNullOrEmpty(objdest.Isuf) ? new XElement(pf + "ISUF", objdest.Isuf.ToString()) : null)));


                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na geração do XML, Regiao XML_Dest - " + x.Message);
                    }

                    #endregion

                    #region Det
                    List<XElement> lcondet = new List<XElement>();
                    try
                    {
                        List<belDet> objdet = nota.det;
                        #region XML_Detalhes

                        List<string> lCfopNotTagII = new List<string>();
                        lCfopNotTagII.Add("3201");
                        lCfopNotTagII.Add("3202");
                        lCfopNotTagII.Add("3211");
                        lCfopNotTagII.Add("3503");
                        lCfopNotTagII.Add("3553");



                        foreach (belDet det in objdet)
                        {
                            //det.imposto.belIpi = null;
                            XElement condet = (new XElement(pf + "det", new XAttribute("nItem", det.nitem),
                                               new XElement(pf + "prod",
                                                   (!String.IsNullOrEmpty(det.prod.Cprod) ? new XElement(pf + "cProd", det.prod.Cprod.ToString().Replace(" ", "")) : null),
                                                   (new XElement(pf + "cEAN", det.prod.Cean.ToString())),
                                                   (!String.IsNullOrEmpty(det.prod.Xprod) ? new XElement(pf + "xProd", det.prod.Xprod.ToString()) : null),
                                                   (!String.IsNullOrEmpty(det.prod.Ncm) ? new XElement(pf + "NCM", det.prod.Ncm.ToString()) : new XElement(pf + "NCM", det.prod.Ncm = "00000000")), //Claudinei - o.s. 24200 - 01/03/2010
                                                   (!String.IsNullOrEmpty(det.prod.Extipi) ? new XElement(pf + "EXTIPI", det.prod.Extipi.ToString()) : null),
                                                   (!String.IsNullOrEmpty(det.prod.Genero) ? new XElement(pf + "genero", det.prod.Genero.ToString()) : null),
                                                   (!String.IsNullOrEmpty(det.prod.Cfop) ? new XElement(pf + "CFOP", det.prod.Cfop.ToString()) : null),
                                                   (!String.IsNullOrEmpty(det.prod.Ucom) ? new XElement(pf + "uCom", det.prod.Ucom.ToString()) : null),
                                                   (new XElement(pf + "qCom", det.prod.Qcom.ToString("#0.0000").Replace(",", "."))),
                                                   (new XElement(pf + "vUnCom", det.prod.Vuncom.ToString("#0." + sCasasVlUnit).Replace(",", "."))),
                                                   (new XElement(pf + "vProd", det.prod.Vprod.ToString("#0.00").Replace(",", "."))),
                                                   (new XElement(pf + "cEANTrib", det.prod.Ceantrib.ToString())),
                                                   (!String.IsNullOrEmpty(det.prod.Utrib) ? new XElement(pf + "uTrib", det.prod.Utrib.ToString()) : null),
                                                   (new XElement(pf + "qTrib", det.prod.Qtrib.ToString("#0.0000").Replace(",", "."))),
                                                   (new XElement(pf + "vUnTrib", det.prod.Vuntrib.ToString("#0." + sCasasVlUnit).Replace(",", "."))),
                                                   (det.prod.Vfrete != 0 ? new XElement(pf + "vFrete", det.prod.Vfrete.ToString("#0.00").Replace(",", ".")) : null),
                                                   (det.prod.Vseg != 0 ? new XElement(pf + "vSeg", det.prod.Vseg.ToString("#0.00").Replace(",", ".")) : null),
                                                   (det.prod.Vdesc > 0 ? new XElement(pf + "vDesc", det.prod.Vdesc.ToString("#0.00").Replace(",", ".")) : null),
                                                   (det.prod.VOutro != 0 ? new XElement(pf + "vOutro", det.prod.VOutro.ToString("#0.00").Replace(",", ".")) : null), //NFe_2.0 
                                                   (det.prod.IndTot != null ? new XElement(pf + "indTot", det.prod.IndTot.ToString()) : null), //NFe_2.0                                                   
                                                   ((det.prod.belDI != null) ? from DI in det.prod.belDI
                                                                               where DI.nDI != null
                                                                                     && DI.xLocDesemb != null
                                                                                     && DI.UFDesemb != null
                                                                                     && DI.cExportador != null
                                                                               select new XElement(pf + "DI",
                                                                                  (DI.nDI != "" ? new XElement(pf + "nDI", DI.nDI) : null),
                                                                                   new XElement(pf + "dDI", DI.DDI.ToString("yyyy-MM-dd")),
                                                                                  (DI.xLocDesemb != "" ? new XElement(pf + "xLocDesemb", DI.xLocDesemb) : null),
                                                                                  (DI.UFDesemb != "" ? new XElement(pf + "UFDesemb", DI.UFDesemb) : null),
                                                                                   new XElement(pf + "dDesemb", DI.dDesemb.ToString("yyyy-MM-dd")),
                                                                                  (DI.cExportador != "" ? new XElement(pf + "cExportador", DI.cExportador) : null),
                                                                                   from adic in DI.adi
                                                                                   where adic.cFabricante != null
                                                                                   select new XElement(pf + "adi",
                                                                                       new XElement(pf + "nAdicao", adic.nAdicao),
                                                                                       new XElement(pf + "nSeqAdic", adic.nSeqAdic),
                                                                                      (adic.cFabricante != "" ? new XElement(pf + "cFabricante", adic.cFabricante) : null),
                                                                                       new XElement(pf + "vDescDI", adic.vDescDI))) : null),
                                                 ((det.prod.XPed != "") ? new XElement(pf + "xPed", det.prod.XPed) : null),
                                                 ((det.prod.NItemPed != "") ? new XElement(pf + "nItemPed", det.prod.NItemPed) : null),
                                                 (det.prod.nFCI != "" ? new XElement(pf + "nFCI", det.prod.nFCI.ToString()) : null), //29280

                                                 (det.prod.belMed != null ?
                                                            from med in det.prod.belMed
                                                            select new XElement(pf + "med",
                                                                            new XElement(pf + "nLote", med.Nlote),
                                                                            new XElement(pf + "qLote", Convert.ToDecimal(med.Qlote).ToString("#0.000").Replace(",", ".")),
                                                                            new XElement(pf + "dFab", med.DFab.ToString("yyyy-MM-dd")),
                                                                            new XElement(pf + "dVal", med.Dval.ToString("yyyy-MM-dd")),
                                                                            new XElement(pf + "vPMC", med.Vpmc)) : null),

                                                 (det.prod.belcomb != null ?
                                                        new XElement(pf + "comb",
                                                                            new XElement(pf + "cProdANP", det.prod.belcomb.cProdANP),
                                                                            new XElement(pf + "UFCons", det.prod.belcomb.UFCons)) : null)),

                                                                                          new XElement(pf + "imposto",
                                new XElement(pf + "vTotTrib", det.prod.vTotTrib.ToString("#0.00").Replace(",", ".")),

                                                   //---------------ICMS-----------------//

                                                   new XElement(pf + "ICMS",

                                                       //-------------ICMS00-------------//

                                                       (det.imposto.belIcms.belIcms00 != null ?
                                                       new XElement(pf + "ICMS00",
                                                            (det.imposto.belIcms.belIcms00.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms00.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms00.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms00.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms00.Modbc != null ? new XElement(pf + "modBC", det.imposto.belIcms.belIcms00.Modbc.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms00.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIcms.belIcms00.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms00.Picms != null ? new XElement(pf + "pICMS", det.imposto.belIcms.belIcms00.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms00.Vicms != null ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms00.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS10-------------//

                                                        (det.imposto.belIcms.belIcms10 != null ?
                                                        new XElement(pf + "ICMS10",
                                                            (det.imposto.belIcms.belIcms10.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms10.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms10.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms10.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms10.Modbc != null ? new XElement(pf + "modBC", det.imposto.belIcms.belIcms10.Modbc.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms10.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIcms.belIcms10.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms10.Picms != null ? new XElement(pf + "pICMS", det.imposto.belIcms.belIcms10.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms10.Vicms != null ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms10.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms10.Modbcst != null ? new XElement(pf + "modBCST", det.imposto.belIcms.belIcms10.Modbcst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms10.Pmvast != 0 ? new XElement(pf + "pMVAST", det.imposto.belIcms.belIcms10.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 11/03/2010
                                                            (det.imposto.belIcms.belIcms10.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.imposto.belIcms.belIcms10.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms10.Vbcst != null ? new XElement(pf + "vBCST", det.imposto.belIcms.belIcms10.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms10.Picmsst != null ? new XElement(pf + "pICMSST", det.imposto.belIcms.belIcms10.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms10.Vicmsst != null ? new XElement(pf + "vICMSST", det.imposto.belIcms.belIcms10.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS20-------------//

                                                        (det.imposto.belIcms.belIcms20 != null ?
                                                        new XElement(pf + "ICMS20",
                                                            (det.imposto.belIcms.belIcms20.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms20.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms20.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms20.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms20.Modbc != null ? new XElement(pf + "modBC", det.imposto.belIcms.belIcms20.Modbc.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms20.Predbc != null ? new XElement(pf + "pRedBC", det.imposto.belIcms.belIcms20.Predbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms20.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIcms.belIcms20.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms20.Picms != null ? new XElement(pf + "pICMS", det.imposto.belIcms.belIcms20.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms20.Vicms != null ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms20.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS30-------------//

                                                        (det.imposto.belIcms.belIcms30 != null ?
                                                        new XElement(pf + "ICMS30",
                                                            (det.imposto.belIcms.belIcms30.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms30.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms30.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms30.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms30.Modbcst != null ? new XElement(pf + "modBCST", det.imposto.belIcms.belIcms30.Modbcst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms30.Pmvast != 0 ? new XElement(pf + "pMVAST", det.imposto.belIcms.belIcms30.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 12/03/2010
                                                            (det.imposto.belIcms.belIcms30.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.imposto.belIcms.belIcms30.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms30.Vbcst != null ? new XElement(pf + "vBCST", det.imposto.belIcms.belIcms30.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms30.Picmsst != null ? new XElement(pf + "pICMSST", det.imposto.belIcms.belIcms30.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms30.Vicmsst != null ? new XElement(pf + "vICMSST", det.imposto.belIcms.belIcms30.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS40-------------//

                                                        (det.imposto.belIcms.belIcms40 != null ?
                                                        new XElement(pf + "ICMS40",
                                                            (det.imposto.belIcms.belIcms40.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms40.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms40.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms40.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms40.Vicms > 0 ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms40.Vicms.ToString("#0.00").Replace(",", ".")) : null), //NFe_2.0
                                                            (det.imposto.belIcms.belIcms40.Vicms > 0 ? new XElement(pf + "motDesICMS", det.imposto.belIcms.belIcms40.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS41-------------//

                                                        (det.imposto.belIcms.belIcms41 != null ?
                                                        new XElement(pf + "ICMS41",
                                                            (det.imposto.belIcms.belIcms41.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms41.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms41.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms41.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms41.Vicms > 0 ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms41.Vicms.ToString("#0.00").Replace(",", ".")) : null),//NFe_2.0
                                                            (det.imposto.belIcms.belIcms41.motDesICMS > 0 ? new XElement(pf + "motDesICMS", det.imposto.belIcms.belIcms41.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS50-------------//

                                                        (det.imposto.belIcms.belIcms50 != null ?
                                                        new XElement(pf + "ICMS50",
                                                            (det.imposto.belIcms.belIcms50.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms50.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms50.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms50.Cst.ToString()) : null),
                                                                (det.imposto.belIcms.belIcms50.Vicms > 0 ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms50.Vicms.ToString("#0.00").Replace(",", ".")) : null),//NFe_2.0
                                                            (det.imposto.belIcms.belIcms50.motDesICMS > 0 ? new XElement(pf + "motDesICMS", det.imposto.belIcms.belIcms50.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS51-------------//

                                                        (det.imposto.belIcms.belIcms51 != null ?
                                                        new XElement(pf + "ICMS51",
                                                            (det.imposto.belIcms.belIcms51.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms51.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms51.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms51.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms51.Modbc != null ? new XElement(pf + "modBC", det.imposto.belIcms.belIcms51.Modbc.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms51.Predbc != null ? new XElement(pf + "pRedBC", det.imposto.belIcms.belIcms51.Predbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms51.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIcms.belIcms51.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms51.Picms != null ? new XElement(pf + "pICMS", det.imposto.belIcms.belIcms51.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms51.Vicms != null ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms51.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS60-------------//

                                                        (det.imposto.belIcms.belIcms60 != null ?
                                                        new XElement(pf + "ICMS60",//Danner - o.s. sem - 12/03/2010
                                                            (det.imposto.belIcms.belIcms60.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms60.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms60.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms60.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms60.Vbcstret != null ? new XElement(pf + "vBCSTRet", det.imposto.belIcms.belIcms60.Vbcstret.ToString("#0.00").Replace(",", ".")) : null),//NFe_2.0 - Mudança de nome de Tag
                                                            (det.imposto.belIcms.belIcms60.Vicmsstret != null ? new XElement(pf + "vICMSSTRet", det.imposto.belIcms.belIcms60.Vicmsstret.ToString("#0.00").Replace(",", ".")) : null)) : null),//NFe_2.0 Mudança de nome de Tag

                                                        //-------------ICMS70-------------//

                                                        (det.imposto.belIcms.belIcms70 != null ?
                                                        new XElement(pf + "ICMS70",
                                                            (det.imposto.belIcms.belIcms70.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms70.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms70.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms70.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms70.Modbc != null ? new XElement(pf + "modBC", det.imposto.belIcms.belIcms70.Modbc.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms70.Predbc != null ? new XElement(pf + "pRedBC", det.imposto.belIcms.belIcms70.Predbc.ToString("#0.00").Replace(',', '.')) : null), //Danner - o.s. 24091 - 06/02/2010
                                                            (det.imposto.belIcms.belIcms70.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIcms.belIcms70.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms70.Picms != null ? new XElement(pf + "pICMS", det.imposto.belIcms.belIcms70.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms70.Vicms != null ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms70.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms70.Modbcst != null ? new XElement(pf + "modBCST", det.imposto.belIcms.belIcms70.Modbcst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms70.Pmvast != 0 ? new XElement(pf + "pMVAST", det.imposto.belIcms.belIcms70.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 12/03/2010
                                                            (det.imposto.belIcms.belIcms70.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.imposto.belIcms.belIcms70.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms70.Vbcst != null ? new XElement(pf + "vBCST", det.imposto.belIcms.belIcms70.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms70.Picmsst != null ? new XElement(pf + "pICMSST", det.imposto.belIcms.belIcms70.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms70.Vicmsst != null ? new XElement(pf + "vICMSST", det.imposto.belIcms.belIcms70.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS90-------------//

                                                        (det.imposto.belIcms.belIcms90 != null ?
                                                        new XElement(pf + "ICMS90",
                                                            (det.imposto.belIcms.belIcms90.Orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belIcms90.Orig.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms90.Cst != null ? new XElement(pf + "CST", det.imposto.belIcms.belIcms90.Cst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms90.Modbc != null ? new XElement(pf + "modBC", det.imposto.belIcms.belIcms90.Modbc.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms90.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIcms.belIcms90.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms90.Predbc != 0 ? new XElement(pf + "pRedBC", det.imposto.belIcms.belIcms90.Predbc.ToString("#0.00").Replace(',', '.')) : null), //Danner - o.s. 24091 - 06/02/2010 //Claudinei - o.s. sem - 24/02/2010
                                                            (det.imposto.belIcms.belIcms90.Picms != null ? new XElement(pf + "pICMS", det.imposto.belIcms.belIcms90.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms90.Vicms != null ? new XElement(pf + "vICMS", det.imposto.belIcms.belIcms90.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms90.Modbcst != null ? new XElement(pf + "modBCST", det.imposto.belIcms.belIcms90.Modbcst.ToString()) : null),
                                                            (det.imposto.belIcms.belIcms90.Pmvast != 0 ? new XElement(pf + "pMVAST", det.imposto.belIcms.belIcms90.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. 24076 - 01/02/2010
                                                            (det.imposto.belIcms.belIcms90.Predbcst != 0 ? new XElement(pf + "pRedBCST", det.imposto.belIcms.belIcms90.Predbcst.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. 24076 - 01/02/2010
                                                            (det.imposto.belIcms.belIcms90.Vbcst != null ? new XElement(pf + "vBCST", det.imposto.belIcms.belIcms90.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms90.Picmsst != null ? new XElement(pf + "pICMSST", det.imposto.belIcms.belIcms90.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.imposto.belIcms.belIcms90.Vicmsst != null ? new XElement(pf + "vICMSST", det.imposto.belIcms.belIcms90.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                       //-------------ICMSSN101--------------//
                                                       (det.imposto.belIcms.belICMSSN101 != null ?
                                                            new XElement(pf + "ICMSSN101",
                                                               (det.imposto.belIcms.belICMSSN101.orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belICMSSN101.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.imposto.belIcms.belICMSSN101.CSOSN.ToString()),
                                                               (det.imposto.belIcms.belICMSSN101.pCredSN != null ? new XElement(pf + "pCredSN", det.imposto.belIcms.belICMSSN101.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.imposto.belIcms.belICMSSN101.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.imposto.belIcms.belICMSSN101.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN102--------------//
                                                       (det.imposto.belIcms.belICMSSN102 != null ?
                                                            new XElement(pf + "ICMSSN102",
                                                               (det.imposto.belIcms.belICMSSN102.orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belICMSSN102.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.imposto.belIcms.belICMSSN102.CSOSN.ToString())) : null),

                                                      //-------------ICMSSN201--------------//
                                                       (det.imposto.belIcms.belICMSSN201 != null ?
                                                            new XElement(pf + (det.imposto.belIcms.belICMSSN201.CSOSN.Equals("201") ? "ICMSSN201" : "ICMSSN202"),
                                                               (det.imposto.belIcms.belICMSSN201.orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belICMSSN201.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.imposto.belIcms.belICMSSN201.CSOSN.ToString()),
                                                               (det.imposto.belIcms.belICMSSN201.modBCST != null ? new XElement(pf + "modBCST", det.imposto.belIcms.belICMSSN201.modBCST.ToString()) : null),
                                                               (det.imposto.belIcms.belICMSSN201.pMVAST != 0 ? new XElement(pf + "pMVAST", det.imposto.belIcms.belICMSSN201.pMVAST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.imposto.belIcms.belICMSSN201.pRedBCST.ToString() != "0" ? new XElement(pf + "pRedBCST", det.imposto.belIcms.belICMSSN201.pRedBCST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.imposto.belIcms.belICMSSN201.vBCST != null ? new XElement(pf + "vBCST", det.imposto.belIcms.belICMSSN201.vBCST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.imposto.belIcms.belICMSSN201.pICMSST != null ? new XElement(pf + "pICMSST", det.imposto.belIcms.belICMSSN201.pICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.imposto.belIcms.belICMSSN201.vICMSST != null ? new XElement(pf + "vICMSST", det.imposto.belIcms.belICMSSN201.vICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                                               ((det.imposto.belIcms.belICMSSN201.pCredSN != null && det.imposto.belIcms.belICMSSN201.CSOSN.Equals("201")) ? new XElement(pf + "pCredSN", det.imposto.belIcms.belICMSSN201.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                                               ((det.imposto.belIcms.belICMSSN201.vCredICMSSN != null && det.imposto.belIcms.belICMSSN201.CSOSN.Equals("201")) ? new XElement(pf + "vCredICMSSN", det.imposto.belIcms.belICMSSN201.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN500--------------//
                                                       (det.imposto.belIcms.belICMSSN500 != null ?
                                                            new XElement(pf + "ICMSSN500",
                                                               (det.imposto.belIcms.belICMSSN500.orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belICMSSN500.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.imposto.belIcms.belICMSSN500.CSOSN.ToString()),
                                                               (det.imposto.belIcms.belICMSSN500.vBCSTRet != null ? new XElement(pf + "vBCSTRet", det.imposto.belIcms.belICMSSN500.vBCSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.imposto.belIcms.belICMSSN500.vICMSSTRet != null ? new XElement(pf + "vICMSSTRet", det.imposto.belIcms.belICMSSN500.vICMSSTRet.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN900--------------//
                                //(det.imposto.belIcms.belICMSSN900 != null ?
                                //     new XElement(pf + "ICMSSN900",
                                //        (det.imposto.belIcms.belICMSSN900.orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belICMSSN900.orig.ToString()) : null),
                                //        new XElement(pf + "CSOSN", det.imposto.belIcms.belICMSSN900.CSOSN.ToString())) : null)),
                                //(det.imposto.belIcms.belICMSSN900.modBC != null ? new XElement(pf + "modBC", det.imposto.belIcms.belICMSSN900.modBC.ToString()) : null),
                                //(det.imposto.belIcms.belICMSSN900.vBC != null ? new XElement(pf + "vBC", det.imposto.belIcms.belICMSSN900.vBC.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.pRedBC != null ? new XElement(pf + "pRedBC", det.imposto.belIcms.belICMSSN900.pRedBC.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.pICMS != null ? new XElement(pf + "pICMS", det.imposto.belIcms.belICMSSN900.pICMS.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.vICMS != null ? new XElement(pf + "vICMS", det.imposto.belIcms.belICMSSN900.vICMS.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.modBCST != null ? new XElement(pf + "modBCST", det.imposto.belIcms.belICMSSN900.modBCST.ToString()) : null),
                                //(det.imposto.belIcms.belICMSSN900.pMVAST != null ? new XElement(pf + "pMVAST", det.imposto.belIcms.belICMSSN900.pMVAST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.pRedBCST != null ? new XElement(pf + "pRedBCST", det.imposto.belIcms.belICMSSN900.pRedBCST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.vBCST != null ? new XElement(pf + "vBCST", det.imposto.belIcms.belICMSSN900.vBCST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.pICMSST != null ? new XElement(pf + "pICMSST", det.imposto.belIcms.belICMSSN900.pICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.vICMSST != null ? new XElement(pf + "vICMSST", det.imposto.belIcms.belICMSSN900.vICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.vBCSTRet != null ? new XElement(pf + "vBCSTRet", det.imposto.belIcms.belICMSSN900.vBCSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.vICMSSTRet != null ? new XElement(pf + "vICMSSTRet", det.imposto.belIcms.belICMSSN900.vICMSSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.pCredSN != null ? new XElement(pf + "pCredSN", det.imposto.belIcms.belICMSSN900.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.imposto.belIcms.belICMSSN900.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.imposto.belIcms.belICMSSN900.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null)),

                                (det.imposto.belIcms.belICMSSN900 != null ?
                                                            new XElement(pf + "ICMSSN900",
                                                               (det.imposto.belIcms.belICMSSN900.orig != null ? new XElement(pf + "orig", det.imposto.belIcms.belICMSSN900.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.imposto.belIcms.belICMSSN900.CSOSN.ToString()),
                                                                    (det.imposto.belIcms.belICMSSN900.modBC != null ? new XElement(pf + "modBC", det.imposto.belIcms.belICMSSN900.modBC.ToString()) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.vBC != null ? new XElement(pf + "vBC", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.vBC).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.pRedBC > 0 ? new XElement(pf + "pRedBC", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.pRedBC).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.pICMS != null ? new XElement(pf + "pICMS", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.pICMS).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.vICMS != null ? new XElement(pf + "vICMS", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.vICMS).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.modBCST != null ? new XElement(pf + "modBCST", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.modBCST).ToString()) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.pMVAST > 0 ? new XElement(pf + "pMVAST", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.pMVAST).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.pRedBCST > 0 ? new XElement(pf + "pRedBCST", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.pRedBCST).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.vBCST != null ? new XElement(pf + "vBCST", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.vBCST).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.pICMSST != null ? new XElement(pf + "pICMSST", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.pICMSST).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.vICMSST != null ? new XElement(pf + "vICMSST", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.vICMSST).ToString("#0.00").Replace(",", ".")) : null),
                                                                    //(det.imposto.belIcms.belICMSSN900.vBCSTRet != null ? new XElement(pf + "vBCSTRet", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.vBCSTRet).ToString("#0.00").Replace(",", ".")) : null),
                                                                    //(det.imposto.belIcms.belICMSSN900.vICMSSTRet != null ? new XElement(pf + "vICMSSTRet", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.vICMSSTRet).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.pCredSN != null ? new XElement(pf + "pCredSN", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.pCredSN).ToString("#0.00").Replace(",", ".")) : null),
                                                                    (det.imposto.belIcms.belICMSSN900.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", Convert.ToDecimal(det.imposto.belIcms.belICMSSN900.vCredICMSSN).ToString("#0.00").Replace(",", ".")) : null)) : null)),

                                        //---------------IPI-------------//
                                            (det.imposto.belIpi != null ?
                                            new XElement(pf + "IPI",
                                                (det.imposto.belIpi.Clenq != null ?
                                                    new XElement(pf + "clEnq", det.imposto.belIpi.Clenq.ToString()) : null),
                                                (det.imposto.belIpi.Cnpjprod != null ?
                                                    new XElement(pf + "CNPJProd", det.imposto.belIpi.Cnpjprod.ToString()) : null),
                                                (det.imposto.belIpi.Cselo != null ?
                                                    new XElement(pf + "cSelo", det.imposto.belIpi.Cselo.ToString()) : null),
                                                (det.imposto.belIpi.Qselo != null ?
                                                    new XElement(pf + "qSelo", det.imposto.belIpi.Qselo.ToString()) : null),
                                                (det.imposto.belIpi.Cenq != null ?
                                                    new XElement(pf + "cEnq", det.imposto.belIpi.Cenq.ToString()) : null),

                                                //-----------IPITrib-----------//    

                                                (det.imposto.belIpi.belIpitrib != null ?
                                                new XElement(pf + "IPITrib",
                                                    (det.imposto.belIpi.belIpitrib.Cst != null ? new XElement(pf + "CST", det.imposto.belIpi.belIpitrib.Cst.ToString()) : null),
                                                    (det.imposto.belIpi.belIpitrib.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIpi.belIpitrib.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                    (det.imposto.belIpi.belIpitrib.Qunid > 0 ? new XElement(pf + "qUnid", det.imposto.belIpi.belIpitrib.Qunid.ToString("#0.0000").Replace(",", ".")) : null),
                                                    (det.imposto.belIpi.belIpitrib.Vunid > 0 ? new XElement(pf + "vUnid", det.imposto.belIpi.belIpitrib.Vunid.ToString("#0.0000").Replace(",", ".")) : null),
                                                    (det.imposto.belIpi.belIpitrib.Pipi != null ? new XElement(pf + "pIPI", det.imposto.belIpi.belIpitrib.Pipi.ToString("#0.00").Replace(",", ".")) : null),
                                                    (det.imposto.belIpi.belIpitrib.Vipi != null ? new XElement(pf + "vIPI", det.imposto.belIpi.belIpitrib.Vipi.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                //-----------IPINT-----------//

                                                (det.imposto.belIpi.belIpint != null ?
                                                new XElement(pf + "IPINT",
                                                    (det.imposto.belIpi.belIpint != null ? new XElement(pf + "CST", det.imposto.belIpi.belIpint.Cst.ToString()) : null)) : null)) : null),




                                       //--------------II--------------//             
                                       ((!lCfopNotTagII.Contains(det.prod.Cfop)) ?
                                       new XElement(pf + "II",
                                           (det.imposto.belIi.Vbc != null ? new XElement(pf + "vBC", det.imposto.belIi.Vbc.ToString("#0.00").Replace(',', '.')) : null),
                                           (det.imposto.belIi.Vdespadu != null ? new XElement(pf + "vDespAdu", det.imposto.belIi.Vdespadu.ToString("#0.00").Replace(',', '.')) : null),
                                           (det.imposto.belIi.Vii != null ? new XElement(pf + "vII", det.imposto.belIi.Vii.ToString("0.00").Replace(',', '.')) : null),
                                           (det.imposto.belIi.Viof != null ? new XElement(pf + "vIOF", det.imposto.belIi.Viof.ToString("#0.00").Replace(',', '.')) : null)) : null),



                               //----------------PIS------------//

                                 (det.imposto.belPis != null ?
                                 new XElement(pf + "PIS",

                                     //-----------PISAliq----------//

                                     (det.imposto.belPis.belPisaliq != null ?
                                     new XElement(pf + "PISAliq",
                                         (det.imposto.belPis.belPisaliq.Cst != null ? new XElement(pf + "CST", det.imposto.belPis.belPisaliq.Cst.ToString()) : null),
                                         (det.imposto.belPis.belPisaliq.Vbc != null ? new XElement(pf + "vBC", det.imposto.belPis.belPisaliq.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.imposto.belPis.belPisaliq.Ppis != null ? new XElement(pf + "pPIS", det.imposto.belPis.belPisaliq.Ppis.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.imposto.belPis.belPisaliq.Vpis != null ? new XElement(pf + "vPIS", det.imposto.belPis.belPisaliq.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                    //-----------PISQtde-----------//                               
                                     (det.imposto.belPis.belPisqtde != null ?
                                     new XElement(pf + "PISQtde",
                                         (det.imposto.belPis.belPisqtde.Cst != null ? new XElement(pf + "CST", det.imposto.belPis.belPisqtde.Cst.ToString()) : null),
                                         (det.imposto.belPis.belPisqtde.Qbcprod != 0 ? new XElement(pf + "qBCProd", det.imposto.belPis.belPisqtde.Qbcprod.ToString()) : null),
                                         (det.imposto.belPis.belPisqtde.Valiqprod != null ? new XElement(pf + "vAliqProd", det.imposto.belPis.belPisqtde.Valiqprod.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.imposto.belPis.belPisqtde.Vpis != null ? new XElement(pf + "vPIS", det.imposto.belPis.belPisqtde.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                     //----------PISNT------------//

                                     (det.imposto.belPis.belPisnt != null ?
                                     new XElement(pf + "PISNT",
                                         (det.imposto.belPis.belPisnt.Cst != null ? new XElement(pf + "CST", det.imposto.belPis.belPisnt.Cst.ToString()) : null)) : null),

                                     //----------PISOutr-----------//

                                     (det.imposto.belPis.belPisoutr != null ?
                                     new XElement(pf + "PISOutr",
                                         (det.imposto.belPis.belPisoutr.Cst != null ? new XElement(pf + "CST", det.imposto.belPis.belPisoutr.Cst.ToString()) : null),
                                         (det.imposto.belPis.belPisoutr.Vbc != null ? new XElement(pf + "vBC", det.imposto.belPis.belPisoutr.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.imposto.belPis.belPisoutr.Ppis != null ? new XElement(pf + "pPIS", det.imposto.belPis.belPisoutr.Ppis.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.imposto.belPis.belPisoutr.Qbcprod != 0 ? new XElement(pf + "qBCProd", det.imposto.belPis.belPisoutr.Qbcprod.ToString()) : null),
                                         (det.imposto.belPis.belPisoutr.Valiqprod != 0 ? new XElement(pf + "vAliqProd", det.imposto.belPis.belPisoutr.Valiqprod.ToString("#0.0000").Replace(",", ".")) : null),
                                         (det.imposto.belPis.belPisoutr.Vpis != null ? new XElement(pf + "vPIS", det.imposto.belPis.belPisoutr.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null)) : null),







                                 //---------------COFINS---------------//
                                 (det.imposto.belCofins != null ?
                                 new XElement(pf + "COFINS",

                                     //-----------COFINSAliq------------//

                                     (det.imposto.belCofins.belCofinsaliq != null ?
                                     new XElement(pf + "COFINSAliq",
                                         new XElement(pf + "CST", det.imposto.belCofins.belCofinsaliq.Cst.ToString()),
                                         new XElement(pf + "vBC", det.imposto.belCofins.belCofinsaliq.Vbc.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "pCOFINS", det.imposto.belCofins.belCofinsaliq.Pcofins.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.imposto.belCofins.belCofinsaliq.Vcofins.ToString("#0.00").Replace(",", "."))) : null),

                                     //------------COFINSQtde------------//

                                     (det.imposto.belCofins.belCofinsqtde != null ?
                                     new XElement(pf + "COFINSQtde",
                                         new XElement(pf + "CST", det.imposto.belCofins.belCofinsqtde.Cst.ToString()),
                                         new XElement(pf + "pBCProd", det.imposto.belCofins.belCofinsqtde.Qbcprod.ToString()),
                                         new XElement(pf + "vAliqProd", det.imposto.belCofins.belCofinsqtde.Valiqprod.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.imposto.belCofins.belCofinsqtde.Vcofins.ToString("#0.00").Replace(",", "."))) : null),

                                     //------------COFINSNT--------------//

                                     (det.imposto.belCofins.belCofinsnt != null ?
                                     new XElement(pf + "COFINSNT",
                                         (det.imposto.belCofins.belCofinsnt.Cst != null ? new XElement(pf + "CST", det.imposto.belCofins.belCofinsnt.Cst.ToString()) : null)) : null),

                                     //------------COFINSOutr--------------//

                                     (det.imposto.belCofins.belCofinsoutr != null ?
                                     new XElement(pf + "COFINSOutr",
                                         new XElement(pf + "CST", det.imposto.belCofins.belCofinsoutr.Cst.ToString()),
                                         new XElement(pf + "vBC", det.imposto.belCofins.belCofinsoutr.Vbc.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "pCOFINS", det.imposto.belCofins.belCofinsoutr.Pcofins.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.imposto.belCofins.belCofinsoutr.Vcofins.ToString("#0.00").Replace(",", "."))) : null)) : null),


                                 //----------------ISSQN-----------------//

                                 (det.imposto.belIss != null ?
                                 new XElement(pf + "ISSQN",
                                     (det.imposto.belIss.Vbc != 0 ? new XElement(pf + "vBC", det.imposto.belIss.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.imposto.belIss.Valiq != 0 ? new XElement(pf + "vAliq", det.imposto.belIss.Valiq.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.imposto.belIss.Vissqn != 0 ? new XElement(pf + "vISSQN", det.imposto.belIss.Vissqn.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.imposto.belIss.Cmunfg != null ? new XElement(pf + "cMunFG", det.imposto.belIss.Cmunfg.ToString()) : null),
                                     (det.imposto.belIss.Clistserv != 0 ? new XElement(pf + "cListServ", det.imposto.belIss.Clistserv.ToString()) : null),
                                     (det.imposto.belIss.cSitTrib != null ? new XElement(pf + "cSitTrib", det.imposto.belIss.cSitTrib.ToString()) : null)) : null)), // NFe_2.0 Tratar item


                                 //-----------INFADPROD-------------//

                                 (!String.IsNullOrEmpty(det.infAdProd.Infadprid) ?
                                     (new XElement(pf + "infAdProd", det.infAdProd.Infadprid.ToString())) : null)));

                            lcondet.Add(condet);
                        }
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na geração do XML, Regiao XML_Detalhes - " + x.Message);
                    }
                    #endregion

                    //Total
                    XContainer contotal;

                    try
                    {
                        belTotal objtotal = nota.total;
                        #region XML_Total

                        contotal = (new XElement(pf + "total",
                                                (objtotal.belIcmstot != null ? new XElement(pf + "ICMSTot",
                                                      new XElement(pf + "vBC", objtotal.belIcmstot.Vbc.ToString("#0.00").Replace(",", ".")),//Danner - o.s. 24271 - 15/03/2010
                                                      new XElement(pf + "vICMS", objtotal.belIcmstot.Vicms.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vBCST", objtotal.belIcmstot.Vbcst.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vST", objtotal.belIcmstot.Vst.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vProd", objtotal.belIcmstot.Vprod.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vFrete", objtotal.belIcmstot.Vfrete.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vSeg", objtotal.belIcmstot.Vseg.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vDesc", objtotal.belIcmstot.Vdesc.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vII", objtotal.belIcmstot.Vii.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vIPI", objtotal.belIcmstot.Vipi.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vPIS", objtotal.belIcmstot.Vpis.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vCOFINS", objtotal.belIcmstot.Vcofins.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vOutro", objtotal.belIcmstot.Voutro.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vNF", objtotal.belIcmstot.Vnf.ToString("#0.00").Replace(",", "."))
                            , new XElement(pf + "vTotTrib", objtotal.belIcmstot.vTotTrib.ToString("#0.00").Replace(",", "."))
                                                      ) : null),



                                                (objtotal.belIssqntot.Vserv > 0 ? new XElement(pf + "ISSQNtot",
                                                    new XElement(pf + "vServ", objtotal.belIssqntot.Vserv.ToString("#0.00").Replace(",", ".")),
                                                    new XElement(pf + "vBC", objtotal.belIssqntot.Vbc.ToString("#0.00").Replace(",", ".")),
                                                    new XElement(pf + "vISS", objtotal.belIssqntot.Viss.ToString("#0.00").Replace(",", ".")),
                                                    (objtotal.belIssqntot.Vpis != 0 ? new XElement(pf + "vPIS", objtotal.belIssqntot.Vpis.ToString("#0.00").Replace(",", ".")) : null),
                                                    (objtotal.belIssqntot.Vcofins != 0 ? new XElement(pf + "vCOFINS", objtotal.belIssqntot.Vcofins.ToString("#0.00").Replace(",", ".")) : null)

                                                    ) : null)));

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na geração do XML, Regiao XML_Total - " + x.Message);
                    }
                    //Fim - Total

                    //Frete
                    XContainer contransp = null;
                    belTransp objtransp;
                    try
                    {
                        objtransp = nota.transp;
                        #region XML_Transporte
                        contransp = (new XElement(pf + "transp",
                                                   new XElement(pf + "modFrete", objtransp.Modfrete.ToString()),
                                                  (objtransp.Modfrete != "9" ? new XElement(pf + "transporta",
                                                       (!String.IsNullOrEmpty(objtransp.belTransportadora.Cnpj) ? new XElement(pf + "CNPJ", objtransp.belTransportadora.Cnpj.ToString()) :
                                                                                                   (objtransp.belTransportadora.Cpf != null ? new XElement(pf + "CPF", objtransp.belTransportadora.Cpf.ToString()) : null)),
                                                       (!String.IsNullOrEmpty(objtransp.belTransportadora.Xnome) ? new XElement(pf + "xNome", objtransp.belTransportadora.Xnome.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belTransportadora.Ie) ? new XElement(pf + "IE", objtransp.belTransportadora.Ie.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belTransportadora.Xender) ? new XElement(pf + "xEnder", objtransp.belTransportadora.Xender.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belTransportadora.Xmun) ? new XElement(pf + "xMun", objtransp.belTransportadora.Xmun.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belTransportadora.Uf) ? new XElement(pf + "UF", objtransp.belTransportadora.Uf.ToString()) : null)) : null),
                (objtransp.belRetTransp.Cfop != null && objtransp.Modfrete != "9" ? (new XElement(pf + "retTransp",
                                                       new XElement(pf + "vServ", objtransp.belRetTransp.Vserv.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "vBCRet", objtransp.belRetTransp.Vbvret.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "pICMSRet", objtransp.belRetTransp.Picmsret.ToString()),
                                                       new XElement(pf + "vICMSRet", objtransp.belRetTransp.Vicmsret.ToString("#0.00").Replace(',', '.')),
                                                       (!String.IsNullOrEmpty(objtransp.belRetTransp.Cfop) ? new XElement(pf + "CFOP", objtransp.belRetTransp.Cfop.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belRetTransp.Cmunfg) ? new XElement(pf + "cMunFG", objtransp.belRetTransp.Cmunfg.ToString()) : null))) : null),
                (objtransp.belVeicTransp.Placa != null && objtransp.Modfrete != "9" ? (new XElement(pf + "veicTransp",
                                                       (!String.IsNullOrEmpty(objtransp.belVeicTransp.Placa) ? new XElement(pf + "placa", objtransp.belVeicTransp.Placa.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belVeicTransp.Uf) ? new XElement(pf + "UF", objtransp.belVeicTransp.Uf.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belVeicTransp.Rntc) ? new XElement(pf + "RNTC", objtransp.belVeicTransp.Rntc.ToString()) : null))) : null),
                   (objtransp.belReboque.Placa != null && objtransp.Modfrete != "9" ? new XElement(pf + "reboque",
                                                       (!String.IsNullOrEmpty(objtransp.belReboque.Placa) ? new XElement(pf + "placa", objtransp.belReboque.Placa.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belReboque.Uf) ? new XElement(pf + "UF", objtransp.belReboque.Uf.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belReboque.Rntc) ? new XElement(pf + "RNTC", objtransp.belReboque.Rntc.ToString()) : null)) : null),
                       (objtransp.belVol != null && objtransp.Modfrete != "9" ? new XElement(pf + "vol",
                                                       (objtransp.belVol.Qvol > 0 ? new XElement(pf + "qVol", Convert.ToInt32(objtransp.belVol.Qvol).ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belVol.Esp) ? new XElement(pf + "esp", objtransp.belVol.Esp.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belVol.Marca) ? new XElement(pf + "marca", objtransp.belVol.Marca.ToString()) : null),
                                                       (!String.IsNullOrEmpty(objtransp.belVol.Nvol) ? new XElement(pf + "nVol", objtransp.belVol.Nvol.ToString()) : null),
                                                       new XElement(pf + "pesoL", objtransp.belVol.PesoL.ToString("#0.000").Replace(",", ".")),
                                                       new XElement(pf + "pesoB", objtransp.belVol.PesoB.ToString("#0.000").Replace(",", "."))) : null),
                   (objtransp.belLacres.Nlacre != null && objtransp.Modfrete != "9" ? new XElement(pf + "lacres",
                                                       new XElement(pf + "nLacre", "")) : null)));



                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + " - Erro na geração do XML, Regiao XML_Transporte - " + x.Message);
                    }
                    //Fim - Frete

                    //Duplicata
                    XContainer concobr = null;
                    belCobr objcob;
                    try
                    {
                        objcob = nota.cobr;
                        #region XML_Cobrança
                        if (Acesso.NM_EMPRESA != "LORENZON")
                        {
                            if (objcob.Fat != null)
                            {
                                concobr = (new XElement(pf + "cobr",
                                                          new XElement(pf + "fat",
                                                              new XElement(pf + "nFat", objcob.Fat.Nfat.ToString()),
                                                              (objcob.Fat.Vorig != 0 ? new XElement(pf + "vOrig", objcob.Fat.Vorig.ToString("#0.00").Replace(",", ".")) : null),
                                                              (objcob.Fat.Vdesc != null && objcob.Fat.Vdesc != 0 ? new XElement(pf + "vDesc", objcob.Fat.Vdesc.ToString("#0.00").Replace(",", ".")) : null),
                                                              (objcob.Fat.Vliq != 0 ? new XElement(pf + "vLiq", objcob.Fat.Vliq.ToString("#0.00").Replace(",", ".")) : null)),
                                                              (objcob.Fat.belDup != null ? from dup in objcob.Fat.belDup
                                                                                           select new XElement(pf + "dup", new XElement(pf + "nDup", dup.Ndup.ToString()),
                                                                                                  new XElement(pf + "dVenc", dup.Dvenc.ToString("yyyy-MM-dd")),
                                                                                                  (Acesso.NM_EMPRESA != "LORENZON" ? new XElement(pf + "vDup", dup.Vdup.ToString("#0.00").Replace(",", ".")) : null)) : null)));
                            }
                        }

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na geração do XML, Region XML_Cobrança - " + x.Message);
                    }
                    //Fim - Duplicata

                    //Obs
                    XContainer conobs;
                    belInfAdic objobs;
                    try
                    {
                        objobs = nota.infAdic;
                        #region XML_Obs

                        if (sUF.Equals("EX") && objobs.Infcpl == null)
                        {
                            objobs.Infcpl = "SEM OBSERVACAO";
                        }

                        conobs = new XElement(pf + "infAdic",
                                                    (!String.IsNullOrEmpty(objobs.Infcpl) ?
                                                    new XElement(pf + "infCpl", objobs.Infcpl.ToString()) : null));
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na geração do XML,Regiao XML_Obs - " + x.Message);
                    }
                    //Fim - Obs

                    //OS_25679

                    #region Exporta
                    XContainer conExporta = null;

                    belExporta objExporta = nota.exporta;

                    if ((objExporta.Ufembarq != "") && (objExporta.Xlocembarq != ""))
                    {
                        conExporta = new XElement(pf + "exporta",
                                                    new XElement(pf + "UFEmbarq", objExporta.Ufembarq),
                                                    new XElement(pf + "xLocEmbarq", objExporta.Xlocembarq));
                    }

                    #endregion

                    //Uniao dos Containers
                    try
                    {

                        concabec.Add(coninfnfe);
                        coninfnfe.Add(conide);
                        conide.AddAfterSelf(conemit);
                        conemit.AddAfterSelf(condest);
                        condest.AddAfterSelf(contotal);
                        contotal.AddAfterSelf(contransp);

                        if (concobr != null)
                        {
                            contransp.AddAfterSelf(concobr);
                            if (objobs.Infcpl != null)
                                concobr.AddAfterSelf(conobs);
                        }
                        else
                        {
                            if (objobs.Infcpl != null)
                                contransp.AddAfterSelf(conobs);
                        }
                        if (conExporta != null)
                            conobs.AddAfterSelf(conExporta);

                        foreach (XElement x in lcondet)
                        {
                            contotal.AddBeforeSelf(x);
                        }
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro na montagem do XML, União dos Containers - " + x.Message);
                    }
                    try
                    {
                        belAssinaXml Assinatura = new belAssinaXml();
                        string nfe = Assinatura.ConfigurarArquivo(concabec.ToString(), "infNFe", Acesso.cert_NFe);
                        nfes.Add(nfe);
                        XElement xnfe = XElement.Parse(nfe);
                        XDocument xdocsalvanfesemlot = new XDocument(xnfe);

                        DirectoryInfo dPastaData = new DirectoryInfo(Pastas.ENVIO + "\\" + nota.chaveNFe.Substring(5, 4));
                        if (!dPastaData.Exists) { dPastaData.Create(); }
                        if (Acesso.TP_EMIS == 2)
                        {
                            xdocsalvanfesemlot.Save(Pastas.CONTINGENCIA + "\\" + nota.chaveNFe.Replace("NFe", "") + "-nfe.xml");
                        }
                        else
                        {
                            xdocsalvanfesemlot.Save(Pastas.ENVIO + "\\" + nota.chaveNFe.Substring(5, 4) + "\\" + nota.chaveNFe.Replace("NFe", "") + "-nfe.xml");
                        }
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + nota.ide.Cnf + "Erro ao assinar a nfe de sequencia " + nota.ide.Cnf + x.Message);
                    }

                    iCount++;
                }


                string sXmlComp = "";
                foreach (var i in nfes)
                {
                    sXmlComp = sXmlComp + i;
                }
                //Estou inserindo o enviNFe pois se eu transformar o arquivo xml assinado em xml e depois em texto usando um toString,
                //a assinatura se torna invalida. Então depois de assinado do trabalho em forma de texto como xml ateh envia-lo pra fazenda.                        
                string sXmlfull = "<?xml version=\"1.0\" encoding=\"utf-8\"?><enviNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"2.00\"><idLote>" +
                             sNomeArquivo.Substring(7, 15) + "</idLote>" + sXmlComp + "</enviNFe>";


                StreamWriter sw = new StreamWriter(sPathLote);
                sw.Write(sXmlfull);
                sw.Close();

                belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_NFE + "\\enviNFe_v2.00.xsd", sPathLote);

                for (int i = 0; i < lNotas.Count; i++)
                {


                }

            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("'vTotTrib'"))
                    throw ex;
            }

        }
Beispiel #28
0
        public TRetEvento GerarXmlCancelamento(belCancelaCte objCancelaCte)
        {
            try
            {
                //Serialização Cancelamento
                XNamespace pf = "http://www.portalfiscal.inf.br/cte";


                XContainer envCTe = new XElement(pf + "evCancCTe",
                     new XElement(pf + "descEvento", "Cancelamento"),
                     new XElement(pf + "nProt", objCancelaCte.nProt),
                     new XElement(pf + "xJust", objCancelaCte.xJust));


                XmlDocument xmlCanc = new XmlDocument();
                xmlCanc.LoadXml(envCTe.ToString());

                // Serialização Evento
                string sVersao = Acesso.versaoCTe; // "1.00";

                TEvento evento = new TEvento();
                evento.versao = sVersao;
                evento.infEvento = new TEventoInfEvento();
                evento.infEvento.tpEvento = "110111";
                evento.infEvento.nSeqEvento = "1"; // numero de evento
                evento.infEvento.Id = "ID" + evento.infEvento.tpEvento + objCancelaCte.chCTe + evento.infEvento.nSeqEvento.PadLeft(2, '0');
                evento.infEvento.cOrgao = Convert.ToByte(Acesso.cUF);
                evento.infEvento.tpAmb = Acesso.TP_AMB == 1 ? TAmb.Item1 : TAmb.Item2;
                evento.infEvento.CNPJ = Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA);
                evento.infEvento.chCTe = objCancelaCte.chCTe;
                evento.infEvento.dhEvento = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss");
                evento.infEvento.detEvento = new TEventoInfEventoDetEvento();
                evento.infEvento.detEvento.versaoEvento = sVersao;
                evento.infEvento.detEvento.Any = (System.Xml.XmlElement)xmlCanc.DocumentElement;

                string sEvento = "";

                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                nameSpaces.Add("", "");
                nameSpaces.Add("", "http://www.portalfiscal.inf.br/cte");


                //SerializeClassToXml.SerializeClasse(TEvento,)



                XmlSerializer xs = new XmlSerializer(typeof(TEvento));
                MemoryStream memory = new MemoryStream();
                XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);
                xs.Serialize(xmltext, evento, nameSpaces);
                UTF8Encoding encoding = new UTF8Encoding();
                sEvento = encoding.GetString(memory.ToArray());
                sEvento = sEvento.Substring(1);


                belAssinaXml Assinatura = new belAssinaXml();
                sEvento = Assinatura.ConfigurarArquivo(sEvento, "infEvento", Acesso.cert_CTe);

                //string sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envEvento xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"1.00\"><idLote>" + nota.sCD_NFSEQ.PadLeft(15, '0')
                //                    + "</idLote>" + sEvento.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "") + "</envEvento>";

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(sEvento);
                string sPath = Pastas.PROTOCOLOS + "\\" + objCancelaCte.chCTe + "_ped-can.xml";
                if (File.Exists(sPath))
                {
                    File.Delete(sPath);
                }
                xDoc.Save(sPath);
                try
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/cte", Pastas.SCHEMA_CTE + "\\eventoCTe_v2.00.xsd", sPath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                #region Cancelar Cte


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

                string sRetorno = CancelarCte(doc);

                XDocument xRet = XDocument.Parse(sRetorno);
                xRet.Save(sPath);
                HLP.GeraXml.bel.CTe.Evento.TRetEvento retorno = SerializeClassToXml.DeserializeClasse<HLP.GeraXml.bel.CTe.Evento.TRetEvento>(sPath);

                //List<belStatusCte> ListaStatus = TrataDadosRetorno(sRetorno);

                //foreach (belStatusCte cte in ListaStatus)
                //{
                if (retorno.infEvento.cStat != "135")
                {
                    File.Delete(sPath);
                }
                //}

                #endregion

                return retorno;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #29
0
        private string GetMsgDados()
        {
            string sVersao = "1.00";
            belEnvEvento objEnvEvento = new belEnvEvento();
            objEnvEvento.idLote = Util.GetNumeroNFe(this.xChaveNFe).PadLeft(15, '0');
            objEnvEvento.versao = sVersao;
            Evento evento = new Evento();
            evento.versao = sVersao;
            evento.infEvento = new eventoInfEvento();
            evento.infEvento.tpEvento = this.xCodEvento;
            evento.infEvento.nSeqEvento = this.iNumEvento.ToString();

            evento.infEvento.nSeqEvento = iNumEvento.ToString(); // numero de evento
            evento.infEvento.Id = "ID" + evento.infEvento.tpEvento + this.xChaveNFe + evento.infEvento.nSeqEvento.PadLeft(2, '0');
            evento.infEvento.cOrgao = 91;
            evento.infEvento.tpAmb = Convert.ToByte(Acesso.TP_AMB);
            evento.infEvento.CNPJ = Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA);
            evento.infEvento.chNFe = this.xChaveNFe;
            evento.infEvento.dhEvento = daoUtil.GetDateServidor().ToString("yyyy-MM-ddTHH:mm:ss" + Acesso.FUSO);
            evento.infEvento.verEvento = sVersao;
            evento.infEvento.detEvento = new eventoInfEventoDetEvento();
            evento.infEvento.detEvento.versao = sVersao;
            evento.infEvento.detEvento.descEvento = lTpEventos.FirstOrDefault(c => c.Key == this.xCodEvento).Value;
            evento.infEvento.detEvento.nProt = this.xProt;
            evento.infEvento.detEvento.xJust = this.xJust != "" ? this.xJust : null;

            string sEvento = "";

            XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
            nameSpaces.Add("", "");
            nameSpaces.Add("", "http://www.portalfiscal.inf.br/nfe");


            XmlSerializer xs = new XmlSerializer(typeof(Evento));
            MemoryStream memory = new MemoryStream();
            XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);
            xs.Serialize(xmltext, evento, nameSpaces);
            UTF8Encoding encoding = new UTF8Encoding();
            sEvento = encoding.GetString(memory.ToArray());
            sEvento = sEvento.Substring(1);



            belAssinaXml Assinatura = new belAssinaXml();
            sEvento = Assinatura.ConfigurarArquivo(sEvento, "infEvento", Acesso.cert_NFe);

            string sXMLfinal = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envEvento xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"1.00\"><idLote>" + objEnvEvento.idLote
                                + "</idLote>" + sEvento.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "") + "</envEvento>";

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sXMLfinal);

            string sPath = "";

            if (this.tpEvento == tipoEvento.CANCELAMENTO)
            {
                sPath = Pastas.PROTOCOLOS + "\\" + objEnvEvento.idLote + "_ped-can.xml";
            }
            else if (tpEvento == tipoEvento.MANIFESTO)
            {
                sPath = Pastas.PROTOCOLOS + "\\" + objEnvEvento.idLote + "_" + this.xCodEvento + "_maniEvent.xml";
            }


            if (File.Exists(sPath))
            {
                File.Delete(sPath);
            }
            xDoc.Save(sPath);
            try
            {
                if (this.tpEvento == tipoEvento.CANCELAMENTO)
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_CANC + "\\envEventoCancNFe_v1.00.xsd", sPath);
                }
                else if (tpEvento == tipoEvento.MANIFESTO)
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_MANIFESTACAO + "\\envConfRecebto_v1.00.xsd", sPath);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return sXMLfinal;
        }
Beispiel #30
0
        private void NfeDadosMgs()
        {
            string _ano = daoUtil.GetDateServidor().ToString("yy");
            _id = Acesso.cUF + _ano + Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA) + _mod + _serie + _nnfini + _nnffim;
            try
            {
                XDocument xDados = new XDocument(new XElement(pf + "inutNFe", new XAttribute("versao", Acesso.versaoNFe),
                                                    new XElement(pf + "infInut", new XAttribute("Id", "ID" + _id),
                                                        new XElement(pf + "tpAmb", Acesso.TP_AMB),
                                                        new XElement(pf + "xServ", "INUTILIZAR"),
                                                        new XElement(pf + "cUF", Acesso.cUF),
                                                        new XElement(pf + "ano", daoUtil.GetDateServidor().ToString("yy")),
                                                        new XElement(pf + "CNPJ", Util.RetiraCaracterCNPJ(Acesso.CNPJ_EMPRESA)),
                                                        new XElement(pf + "mod", _mod),
                                                        new XElement(pf + "serie", Convert.ToInt16(_serie)),
                                                        new XElement(pf + "nNFIni", Convert.ToInt32(_nnfini)),
                                                        new XElement(pf + "nNFFin", Convert.ToInt32(_nnffim)),
                                                        new XElement(pf + "xJust", _sjust))));

                _sPath = Pastas.PROTOCOLOS + _id + "_ped_inu.xml";
                xDados.Save(Pastas.PROTOCOLOS + _id + "_ped_inu.xml");
                belAssinaXml assina = new belAssinaXml();
                assina.Assinar(_sPath, "infInut", Acesso.cert_NFe);
                belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/nfe", Pastas.SCHEMA_NFE + "\\inutNFe_v2.00.xsd", _sPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private string MontaXmlCancelamentoHomo(TcPedidoCancelamento objPedCanc)
        {
            try
            {
                XNamespace pf = "http://www.ginfes.com.br/servico_cancelar_nfse_envio";
                XContainer conCancelarNfseEnvio = null;
                conCancelarNfseEnvio = (new XElement(pf + "CancelarNfseEnvio", new XAttribute("xmlns", "http://www.ginfes.com.br/servico_cancelar_nfse_envio"),
                                        new XElement(pf + "Prestador", new XElement(pf + "Cnpj", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Cnpj),
                                                                      ((objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.InscricaoMunicipal != "") ? new XElement(pf + "InscricaoMunicipal", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.InscricaoMunicipal) : null)),
                                        new XElement(pf + "NumeroNfse", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero)));


                belAssinaXml Assinatura = new belAssinaXml();
                string sArquivo = Assinatura.ConfigurarArquivo(conCancelarNfseEnvio.ToString(), "NumeroNfse", Acesso.cert_NFs);

                DirectoryInfo dPastaData = new DirectoryInfo(Pastas.PROTOCOLOS + "\\Servicos");
                if (!dPastaData.Exists) { dPastaData.Create(); }
                XmlDocument xdocCanc = new XmlDocument();
                xdocCanc.LoadXml(sArquivo);
                string sCaminho = Pastas.PROTOCOLOS + "\\Servicos\\ped_canc_" + objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero + ".xml";
                xdocCanc.Save(sCaminho);


                //belValidaXml.ValidarXml("http://www.ginfes.com.br/servico_cancelar_nfse_envio.xsd", Pastas.SCHEMA_NFSE + "\\servico_cancelar_nfse_envio.xsd", sCaminho);

                sArquivo = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + sArquivo;

                return sArquivo;
            }
            catch (Exception ex)
            {
                return "";
                throw ex;
            }
        }
        private string MontaXmlCancelamentoV3(TcPedidoCancelamento objPedCanc)
        {
            try
            {
                XNamespace pf = "http://www.ginfes.com.br/servico_cancelar_nfse_envio_v03.xsd";
                XNamespace tipos = "http://www.ginfes.com.br/tipos_v03.xsd";
                XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

                XContainer conCancelarNfseEnvio = null;
                conCancelarNfseEnvio = (new XElement(pf + "CancelarNfseEnvio", new XAttribute(xsi + "schemaLocation", "http://www.ginfes.com.br/servico_cancelar_nfse_envio_v03.xsd"),
                                                                              new XAttribute("xmlns", "http://www.ginfes.com.br/servico_cancelar_nfse_envio_v03.xsd"),
                                                                              new XAttribute(XNamespace.Xmlns + "tipos", "http://www.ginfes.com.br/tipos_v03.xsd"),
                                                                              new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                                            new XElement(pf + "Pedido",
                                                new XElement(tipos + "InfPedidoCancelamento",
                                                     new XElement(tipos + "IdentificacaoNfse",
                                                         new XElement(tipos + "Numero", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero),
                                                         new XElement(tipos + "Cnpj", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Cnpj),
                                                         new XElement(tipos + "InscricaoMunicipal", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.InscricaoMunicipal),
                                                         new XElement(tipos + "CodigoMunicipio", objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.CodigoMunicipio)),
                                                     new XElement(tipos + "CodigoCancelamento", objPedCanc.InfPedidoCancelamento.CodigoCancelamento)))));



                belAssinaXml Assinatura = new belAssinaXml();
                string sArquivo = Assinatura.ConfigurarArquivo(conCancelarNfseEnvio.ToString(), "tipos:InfPedidoCancelamento", Acesso.cert_NFs);

                DirectoryInfo dPastaData = new DirectoryInfo(Pastas.PROTOCOLOS + "\\Servicos");
                if (!dPastaData.Exists) { dPastaData.Create(); }
                XmlDocument xdocCanc = new XmlDocument();
                xdocCanc.LoadXml(sArquivo);
                string sCaminho = Pastas.PROTOCOLOS + "\\Servicos\\ped_canc_" + objPedCanc.InfPedidoCancelamento.IdentificacaoNfse.Numero + ".xml";
                xdocCanc.Save(sCaminho);


                // belValidaXml.ValidarXml("http://www.ginfes.com.br/servico_cancelar_nfse_envio.xsd", Pastas.SCHEMA_NFSE + "\\servico_cancelar_nfse_envio_v03.xsd", sCaminho);

                return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + sArquivo;
            }
            catch (Exception ex)
            {
                return "";
                throw ex;
            }
        }
Beispiel #33
0
        public void GerarArquivoXml2(tcLoteRps objLoteRpd)
        {
            try
            {
                List <string> ListaNfes = new List <string>();
                XDocument     xdoc      = new XDocument();
                XNamespace    pf        = "http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd";


                XContainer conEnviarLoteRpsEnvio = (new XElement(pf + "EnviarLoteRpsEnvio", new XAttribute("xmlns", "http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd")));

                XContainer conLoteRps = null;
                conLoteRps = (new XElement(pf + "LoteRps", new XAttribute("Id", Convert.ToInt32(objLoteRpd.NumeroLote)),
                                           new XElement(pf + "NumeroLote", objLoteRpd.NumeroLote.ToString()),
                                           new XElement(pf + "Cnpj", objLoteRpd.Cnpj.ToString()),
                                           new XElement(pf + "InscricaoMunicipal", objLoteRpd.InscricaoMunicipal.ToString()),
                                           new XElement(pf + "QuantidadeRps", objLoteRpd.QuantidadeRps.ToString())));

                XContainer conListaRps             = (new XElement(pf + "ListaRps"));
                XContainer conRps                  = (new XElement(pf + "Rps"));
                XContainer conInfRps               = null;
                XContainer conSubstituto           = null;
                XContainer conServico              = null;
                XContainer conPrestador            = null;
                XContainer conTomador              = null;
                XContainer conIntermediarioServico = null;
                XContainer conConstrucaoCivil      = null;


                belAssinaXml Assinatura = new belAssinaXml();

                foreach (TcRps rps in objLoteRpd.Rps)
                {
                    #region IdentificacaoRps conRps
                    try
                    {
                        conInfRps = (new XElement(pf + "InfRps", new XElement(pf + "IdentificacaoRps",
                                                                              new XElement(pf + "Numero", rps.InfRps.IdentificacaoRps.Numero),
                                                                              new XElement(pf + "Serie", rps.InfRps.IdentificacaoRps.Serie),
                                                                              new XElement(pf + "Tipo", rps.InfRps.IdentificacaoRps.Tipo)),
                                                  new XElement(pf + "DataEmissao", daoUtil.GetDateServidor().Date.ToString("yyyy-MM-ddTHH:mm:ss")),
                                                  new XElement(pf + "NaturezaOperacao", rps.InfRps.NaturezaOperacao),
                                                  ((rps.InfRps.RegimeEspecialTributacao != 0) ? new XElement(pf + "RegimeEspecialTributacao", rps.InfRps.RegimeEspecialTributacao) : null),
                                                  new XElement(pf + "OptanteSimplesNacional", rps.InfRps.OptanteSimplesNacional),
                                                  new XElement(pf + "IncentivadorCultural", rps.InfRps.IncentivadorCultural),
                                                  new XElement(pf + "Status", rps.InfRps.Status)));
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('IdentificacaoRps')- "
                                            + Environment.NewLine + "XML_Detalhes: "
                                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region RpsSubstituido
                    if (rps.InfRps.RpsSubstituido != null)
                    {
                        try
                        {
                            conSubstituto = (new XElement("RpsSubstituido",
                                                          new XElement("Numero", rps.InfRps.RpsSubstituido.Numero),
                                                          new XElement("Serie", rps.InfRps.RpsSubstituido.Serie),
                                                          new XElement("Tipo", rps.InfRps.RpsSubstituido.Tipo)));
                        }
                        catch (Exception x)
                        {
                            throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('RpsSubstituido')- "
                                                + Environment.NewLine + "XML_Detalhes: "
                                                + Environment.NewLine + x.Message);
                        }
                    }

                    #endregion

                    #region Servico
                    try
                    {
                        conServico = (new XElement(pf + "Servico", new XElement(pf + "Valores",
                                                                                new XElement(pf + "ValorServicos", rps.InfRps.Servico.Valores.ValorServicos.ToString("#0.00").Replace(",", ".")),
                                                                                ((rps.InfRps.Servico.Valores.ValorDeducoes != 0) ? new XElement(pf + "ValorDeducoes", rps.InfRps.Servico.Valores.ValorDeducoes.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.ValorPis != null) ? new XElement(pf + "ValorPis", rps.InfRps.Servico.Valores.ValorPis.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.ValorCofins != null) ? new XElement(pf + "ValorCofins", rps.InfRps.Servico.Valores.ValorCofins.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.ValorInss != null) ? new XElement(pf + "ValorInss", rps.InfRps.Servico.Valores.ValorInss.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.ValorIr != null) ? new XElement(pf + "ValorIr", rps.InfRps.Servico.Valores.ValorIr.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.ValorCsll != null) ? new XElement(pf + "ValorCsll", rps.InfRps.Servico.Valores.ValorCsll.ToString("#0.00").Replace(",", ".")) : null),
                                                                                new XElement(pf + "IssRetido", rps.InfRps.Servico.Valores.IssRetido),
                                                                                ((rps.InfRps.Servico.Valores.ValorIss != null) ? new XElement(pf + "ValorIss", rps.InfRps.Servico.Valores.ValorIss.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.ValorIssRetido != null) ? new XElement(pf + "ValorIssRetido", rps.InfRps.Servico.Valores.ValorIssRetido.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.OutrasRetencoes != 0) ? new XElement(pf + "OutrasRetencoes", rps.InfRps.Servico.Valores.OutrasRetencoes.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.BaseCalculo != 0) ? new XElement(pf + "BaseCalculo", rps.InfRps.Servico.Valores.BaseCalculo.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.Aliquota != null) ? new XElement(pf + "Aliquota", (rps.InfRps.Servico.Valores.Aliquota / 100).ToString("#0.0000").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.ValorLiquidoNfse != 0) ? new XElement(pf + "ValorLiquidoNfse", rps.InfRps.Servico.Valores.ValorLiquidoNfse.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.DescontoCondicionado != 0) ? new XElement(pf + "DescontoCondicionado", rps.InfRps.Servico.Valores.DescontoCondicionado.ToString("#0.00").Replace(",", ".")) : null),
                                                                                ((rps.InfRps.Servico.Valores.DescontoIncondicionado != 0) ? new XElement(pf + "DescontoIncondicionado", rps.InfRps.Servico.Valores.DescontoIncondicionado.ToString("#0.00").Replace(",", ".")) : null)),
                                                   new XElement(pf + "ItemListaServico", rps.InfRps.Servico.ItemListaServico),
                                                   ((rps.InfRps.Servico.CodigoCnae != "") ? new XElement(pf + "CodigoCnae", rps.InfRps.Servico.CodigoCnae) : null),
                                                   (((rps.InfRps.Servico.CodigoTributacaoMunicipio != "") && (rps.InfRps.Servico.CodigoTributacaoMunicipio != "0")) ? new XElement(pf + "CodigoTributacaoMunicipio", rps.InfRps.Servico.CodigoTributacaoMunicipio) : null),
                                                   new XElement(pf + "Discriminacao", rps.InfRps.Servico.Discriminacao),
                                                   new XElement(pf + "CodigoMunicipio", rps.InfRps.Servico.CodigoMunicipio)));
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('Servico')- "
                                            + Environment.NewLine + "XML_Detalhes: "
                                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region Prestador
                    try
                    {
                        conPrestador = (new XElement(pf + "Prestador", new XElement(pf + "Cnpj", rps.InfRps.Prestador.Cnpj),
                                                     ((rps.InfRps.Prestador.InscricaoMunicipal != "") ? new XElement(pf + "InscricaoMunicipal", rps.InfRps.Prestador.InscricaoMunicipal) : null)));
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('Prestador')- "
                                            + Environment.NewLine + "XML_Detalhes: "
                                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region Tomador
                    try
                    {
                        //conTomador = (new XElement(pf + "Tomador", ((rps.InfRps.Tomador.IdentificacaoTomador != null) ? new XElement(pf + "IdentificacaoTomador", new XElement(pf + "CpfCnpj",
                        //                                                                                       new XElement(pf + (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? "Cnpj" : "Cpf"), (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj : rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cpf))),
                        //                                            (rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal != "" ? (new XElement(pf + "InscricaoMunicipal", rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal)) : null)) : null),
                        //                                            new XElement(pf + "RazaoSocial", rps.InfRps.Tomador.RazaoSocial),
                        //                                            new XElement(pf + "Endereco",
                        //                                                               new XElement(pf + "Endereco", rps.InfRps.Tomador.Endereco.Endereco),
                        //                                                               new XElement(pf + "Numero", rps.InfRps.Tomador.Endereco.Numero),
                        //                                                              (rps.InfRps.Tomador.Endereco.Complemento != "" ? new XElement(pf + "Complemento", rps.InfRps.Tomador.Endereco.Complemento) : null),
                        //                                                               new XElement(pf + "Bairro", rps.InfRps.Tomador.Endereco.Bairro),
                        //                                                               new XElement(pf + "CodigoMunicipio", rps.InfRps.Tomador.Endereco.CodigoMunicipio),
                        //                                                               new XElement(pf + "Uf", rps.InfRps.Tomador.Endereco.Uf),
                        //                                                               new XElement(pf + "Cep", rps.InfRps.Tomador.Endereco.Cep))));

                        conTomador = (new XElement(pf + "Tomador", ((rps.InfRps.Tomador.IdentificacaoTomador != null) ? new XElement(pf + "IdentificacaoTomador", new XElement(pf + "CpfCnpj",
                                                                                                                                                                               new XElement(pf + (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? "Cnpj" : "Cpf"), (rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj != "" ? rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cnpj : rps.InfRps.Tomador.IdentificacaoTomador.CpfCnpj.Cpf))),
                                                                                                                                     (rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal != "" ? (new XElement(pf + "InscricaoMunicipal", rps.InfRps.Tomador.IdentificacaoTomador.InscricaoMunicipal)) : null)) : null),
                                                   new XElement(pf + "RazaoSocial", rps.InfRps.Tomador.RazaoSocial),
                                                   new XElement(pf + "Endereco",
                                                                new XElement(pf + "Endereco", rps.InfRps.Tomador.Endereco.Endereco),
                                                                new XElement(pf + "Numero", rps.InfRps.Tomador.Endereco.Numero),
                                                                (rps.InfRps.Tomador.Endereco.Complemento != "" ? new XElement(pf + "Complemento", rps.InfRps.Tomador.Endereco.Complemento) : null),
                                                                new XElement(pf + "Bairro", rps.InfRps.Tomador.Endereco.Bairro),
                                                                new XElement(pf + "CodigoMunicipio", rps.InfRps.Tomador.Endereco.CodigoMunicipio),
                                                                new XElement(pf + "Uf", rps.InfRps.Tomador.Endereco.Uf),
                                                                new XElement(pf + "Cep", rps.InfRps.Tomador.Endereco.Cep)),
                                                   ((rps.InfRps.Tomador.Contato.Telefone != "" || rps.InfRps.Tomador.Contato.Email != "") ?
                                                    (new XElement(pf + "Contato",
                                                                  ((rps.InfRps.Tomador.Contato.Telefone != "") ? new XElement(pf + "Telefone", rps.InfRps.Tomador.Contato.Telefone) : null),
                                                                  ((rps.InfRps.Tomador.Contato.Email != "") ? new XElement(pf + "Email", rps.InfRps.Tomador.Contato.Email) : null))) : null)));
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('Tomador')- "
                                            + Environment.NewLine + "XML_Detalhes: "
                                            + Environment.NewLine + x.Message);
                    }

                    #endregion

                    #region IntermediarioServico
                    try
                    {
                        if (rps.InfRps.IntermediarioServico != null)
                        {
                            conIntermediarioServico = (new XElement(pf + "IntermediarioServico", new XElement(pf + "RazaoSocial", rps.InfRps.IntermediarioServico.RazaoSocial),
                                                                    new XElement(pf + "CpfCnpj", (new XElement((rps.InfRps.IntermediarioServico.CpfCnpj.Cnpj != null ? "Cnpj" : "Cpf"), (rps.InfRps.IntermediarioServico.CpfCnpj.Cnpj != null ? rps.InfRps.IntermediarioServico.CpfCnpj.Cnpj : rps.InfRps.IntermediarioServico.CpfCnpj.Cpf)))),
                                                                    new XElement(pf + "InscricaoMunicipal", rps.InfRps.IntermediarioServico.InscricaoMunicipal)));
                        }
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('IntermediarioServico')- "
                                            + Environment.NewLine + "XML_Detalhes: "
                                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    #region ConstrucaoCivil

                    try
                    {
                        if (rps.InfRps.ConstrucaoCivil != null)
                        {
                            if ((rps.InfRps.ConstrucaoCivil.CodigoObra != "") && (rps.InfRps.ConstrucaoCivil.Art != ""))
                            {
                                conConstrucaoCivil = (new XElement(pf + "ConstrucaoCivil", new XElement(pf + "CodigoObra", rps.InfRps.ConstrucaoCivil.CodigoObra),
                                                                   new XElement(pf + "Art", rps.InfRps.ConstrucaoCivil.Art)));
                            }
                        }
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota : " + rps.InfRps.IdentificacaoRps.Numero + "Erro na geração do XML, Regiao:('ConstrucaoCivil')- "
                                            + Environment.NewLine + "XML_Detalhes: "
                                            + Environment.NewLine + x.Message);
                    }
                    #endregion

                    try
                    {
                        if (conSubstituto != null)
                        {
                            conInfRps.Add(conSubstituto);
                        }
                        conInfRps.Add(conServico);
                        conInfRps.Add(conPrestador);
                        conInfRps.Add(conTomador);
                        if (conIntermediarioServico != null)
                        {
                            conInfRps.Add(conIntermediarioServico);
                        }
                        if (conConstrucaoCivil != null)
                        {
                            conInfRps.Add(conConstrucaoCivil);
                        }
                        conRps.Add(conInfRps);


                        conListaRps.Add(conRps);


                        //Salva o Rps;
                        XDocument     xdocsalvanfesemlot = new XDocument(conRps);
                        string        sPasta             = rps.InfRps.DataEmissao.ToString("MM/yy").Replace("/", "");
                        string        sNomeArquivo       = rps.InfRps.DataEmissao.ToString("MM/yy").Replace("/", "") + rps.InfRps.IdentificacaoRps.Serie + rps.InfRps.IdentificacaoRps.Numero;
                        DirectoryInfo dPastaData         = new DirectoryInfo(Pastas.ENVIO + "\\Servicos\\" + sPasta);
                        if (!dPastaData.Exists)
                        {
                            dPastaData.Create();
                        }
                        xdocsalvanfesemlot.Save(Pastas.ENVIO + "\\Servicos\\" + sPasta + "\\" + sNomeArquivo + "-nfes.xml");

                        conRps = (new XElement("Rps"));
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + "sNota" + "Erro ao assinar a nfe de sequencia " + "sNota" + x.Message);
                    }
                }

                conLoteRps.Add(conListaRps);
                conEnviarLoteRpsEnvio.Add(conLoteRps);
                //Assina Arquivo
                string nfes = Assinatura.ConfigurarArquivo(conEnviarLoteRpsEnvio.ToString(), "LoteRps", Acesso.cert_NFs);
                sXmlLote = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + nfes;
                //Grava
                string       sPathLote = Pastas.ENVIO + "\\Servicos\\" + "Lote_" + objLoteRpd.NumeroLote + ".xml";
                StreamWriter sw        = new StreamWriter(sPathLote);
                sw.Write(sXmlLote);
                sw.Close();

                #region Valida_Xml


                try
                {
                    if (Acesso.tipoWsNfse == Acesso.TP_WS_NFSE.GINFES)
                    {
                        belValidaXml.ValidarXml("http://www.ginfes.com.br/servico_enviar_lote_rps_envio_v03.xsd", Pastas.SCHEMA_NFSE + "\\servico_enviar_lote_rps_envio_v03.xsd", sPathLote);
                    }
                    else
                    {
                        belValidaXml.ValidarXml("http://www.nfe.com.br/WSNacional/XSD/1/nfse_municipal_v01.xsd", Pastas.SCHEMA_NFSE + "\\TIPLAN\\nfse_municipal_v01.xsd", sPathLote);
                    }
                }
                catch (XmlException x)
                {
                    File.Delete(sPathLote);
                    throw new Exception(x.Message);
                }
                catch (XmlSchemaException x)
                {
                    File.Delete(sPathLote);
                    throw new Exception(x.Message);
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #34
0
        public bool ExecuteEvento()
        {
            try
            {
                XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                nameSpaces.Add("", "");
                nameSpaces.Add("", "http://www.portalfiscal.inf.br/mdfe");

                XmlSerializer xs = new XmlSerializer(typeof(TEvento));
                MemoryStream memory = new MemoryStream();
                XmlTextWriter xmltext = new XmlTextWriter(memory, Encoding.UTF8);
                xs.Serialize(xmltext, evento, nameSpaces);
                UTF8Encoding encoding = new UTF8Encoding();
                string sEvento = encoding.GetString(memory.ToArray());
                sEvento = sEvento.Substring(1);


                belAssinaXml Assinatura = new belAssinaXml();
                sEvento = Assinatura.ConfigurarArquivo(sEvento, "infEvento", Acesso.cert_CTe);

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(sEvento);
                string sPath = Pastas.PROTOCOLOS + "\\" + objPesquisa.chaveMDFe + "_ped-can-mdfe.xml";
                if (File.Exists(sPath))
                {
                    File.Delete(sPath);
                }
                xDoc.Save(sPath);
                try
                {
                    belValidaXml.ValidarXml("http://www.portalfiscal.inf.br/mdfe", Pastas.SCHEMA_MDFe + "\\eventoMDFe_v1.00.xsd", sPath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                #region Cancelar Cte
                XmlDocument doc = new XmlDocument();
                doc.Load(sPath);
                string sRetorno = Execute(doc);
                XDocument xRet = XDocument.Parse(sRetorno);
                xRet.Save(sPath);
                retorno = SerializeClassToXml.DeserializeClasse<TRetEvento>(sPath);
                if (retorno.infEvento.cStat != "135")
                {
                    File.Delete(sPath);
                }
                sMessage = string.Format("Codigo do Retorno: {0}{1}Motivo: {2}{1}Chave: {3}{1}Protocolo: {4}{1}",
                    retorno.infEvento.cStat,
                    Environment.NewLine,
                    retorno.infEvento.xMotivo,
                    retorno.infEvento.chMDFe,
                    retorno.infEvento.nProt);

                if (retorno.infEvento.cStat == "135")
                {
                    return true;
                }
                else
                {
                    return false;
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
Beispiel #35
0
        private string MontaXmlConsultarSituacaoLoteRps(tcIdentificacaoPrestador objPrestador)
        {
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;
            try
            {
                XNamespace tipos = "http://www.ginfes.com.br/tipos_v03.xsd";
                XNamespace pf = "http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd";
                XContainer conPrestador = null;
                XContainer conProtocolo = null;

                XContainer conConsultarLoteRpsEnvio = (new XElement(pf + "ConsultarSituacaoLoteRpsEnvio", new XAttribute("xmlns", "http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd"),
                                                                        new XAttribute(XNamespace.Xmlns + "tipos", "http://www.ginfes.com.br/tipos_v03.xsd")));

                conPrestador = (new XElement(pf + "Prestador",
                    new XElement(tipos + "Cnpj", objPrestador.Cnpj),
                                                                     ((objPrestador.InscricaoMunicipal != "") ? new XElement(tipos + "InscricaoMunicipal", objPrestador.InscricaoMunicipal) : null)));

                conProtocolo = new XElement(pf + "Protocolo", Protocolo);


                conConsultarLoteRpsEnvio.Add(conPrestador);
                conConsultarLoteRpsEnvio.Add(conProtocolo);
                belAssinaXml Assinatura = new belAssinaXml();
                string sArquivo = Assinatura.ConfigurarArquivo(conConsultarLoteRpsEnvio.ToString(), "Protocolo", Acesso.cert_NFs);



                XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                reader = new XmlValidatingReader(sArquivo, XmlNodeType.Element, context);

                myschema.Add("http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd", Pastas.SCHEMA_NFSE + "\\servico_consultar_situacao_lote_rps_envio_v03.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                { }


                return sArquivo;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }