public TcPedidoCancelamento BuscaDadosParaCancelamento(FbConnection Conn, string sCodCancelamento, string sSequencia)
        {
            try
            {
                StringBuilder sQuery = new StringBuilder();
                sQuery.Append("select cidades.cd_municipio, empresa.cd_cgc Cnpj, empresa.cd_inscrmu, ");
                sQuery.Append("coalesce(nf.cd_numero_nfse,'')cd_numero_nfse ");
                sQuery.Append("from nf inner join empresa on nf.cd_empresa = empresa.cd_empresa ");
                sQuery.Append("inner join cidades on (cidades.nm_cidnor = empresa.nm_cidnor) ");
                sQuery.Append("where nf.cd_nfseq = '" + sSequencia + "' and ");
                sQuery.Append("nf.cd_empresa = '" + belStatic.codEmpresaNFe + "'");

                FbCommand Command = new FbCommand(sQuery.ToString(), Conn);
                FbDataReader dr = Command.ExecuteReader();
                dr.Read();

                TcPedidoCancelamento objCancelamento = new TcPedidoCancelamento();
                objCancelamento.InfPedidoCancelamento = new tcInfPedidoCancelamento();
                objCancelamento.InfPedidoCancelamento.CodigoCancelamento = sCodCancelamento;
                objCancelamento.InfPedidoCancelamento.IdentificacaoNfse = new tcIdentificacaoNfse();
                objCancelamento.InfPedidoCancelamento.IdentificacaoNfse.CodigoMunicipio = dr["cd_municipio"].ToString();
                objCancelamento.InfPedidoCancelamento.IdentificacaoNfse.Numero = dr["cd_numero_nfse"].ToString();
                objCancelamento.InfPedidoCancelamento.IdentificacaoNfse.Cnpj = dr["Cnpj"].ToString();
                objCancelamento.InfPedidoCancelamento.IdentificacaoNfse.InscricaoMunicipal = dr["cd_inscrmu"].ToString();

                return objCancelamento;

            }
            catch (Exception)
            {
                throw;
            }
        }
        private string MontaXmlCancelamentoHomo(TcPedidoCancelamento objPedCanc, X509Certificate2 cert)
        {
            try
            {
                XNamespace pf = "http://www.ginfes.com.br/servico_cancelar_nfse_envio";
                //XNamespace tipos = "http://www.ginfes.com.br/tipos";
                XContainer conCancelarNfseEnvio = null;
                conCancelarNfseEnvio = (new XElement(pf + "CancelarNfseEnvio", new XAttribute("xmlns", "http://www.ginfes.com.br/servico_cancelar_nfse_envio"),
                                           // new XAttribute(XNamespace.Xmlns + "tipos", "http://www.ginfes.com.br/tipos"),
                                        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)));

                //Valida

                AssinaNFeXml Assinatura = new AssinaNFeXml();
                string sArquivo = Assinatura.ConfigurarArquivo(conCancelarNfseEnvio.ToString(), "NumeroNfse", cert);

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

                Globais getschema = new Globais();

                XmlSchemaCollection myschema = new XmlSchemaCollection();
                XmlValidatingReader reader;
                XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                sArquivo = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + sArquivo;
                //reader = new XmlValidatingReader(sArquivo, XmlNodeType.Element, context);

                //myschema.Add("http://www.ginfes.com.br/servico_cancelar_nfse_envio", getschema.LeRegConfig("PastaSchema") + "\\servico_cancelar_nfse_envio_v02.xsd");

                //reader.ValidationType = ValidationType.Schema;

                //reader.Schemas.Add(myschema);

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


                return sArquivo;
            }
            catch (Exception ex)
            {
                return "";
                throw;
            }
        }
        private string MontaXmlCancelamentoV3(TcPedidoCancelamento objPedCanc, X509Certificate2 cert)
        {
            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)))));

                //Valida

                AssinaNFeXml Assinatura = new AssinaNFeXml();
                string sArquivo = Assinatura.ConfigurarArquivo(conCancelarNfseEnvio.ToString(), "tipos:InfPedidoCancelamento", cert);

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

                Globais getschema = new Globais();

                XmlSchemaCollection myschema = new XmlSchemaCollection();
                XmlValidatingReader reader;
                XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

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

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

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                {
                }


                return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + sArquivo;
            }
            catch (Exception ex)
            {
                return "";
                throw;
            }
        }
        public string CancelaNfes(TcPedidoCancelamento objPedCanc, X509Certificate2 cert)
        {
            try
            {
                string sRet = "";

                //Homologação
                if (belStatic.tpAmbNFse == 2)
                {
                    HLP.WebService.Itu_servicos_Homologacao.ServiceGinfesImplService objtrans = new HLP.WebService.Itu_servicos_Homologacao.ServiceGinfesImplService();
                    objtrans.ClientCertificates.Add(cert);
                    objtrans.Timeout = 60000;
                    sRet = objtrans.CancelarNfse(MontaXmlCancelamentoHomo(objPedCanc, cert));
                }
                else if (belStatic.tpAmbNFse == 1)
                {
                    HLP.WebService.Itu_servicos_Producao.ServiceGinfesImplService objtrans = new HLP.WebService.Itu_servicos_Producao.ServiceGinfesImplService();
                    objtrans.ClientCertificates.Add(cert);
                    objtrans.Timeout = 60000;
                    sRet = objtrans.CancelarNfse(MontaXmlCancelamento(objPedCanc, cert));
                    // sRet = objtrans.CancelarNfse(MontaXmlCancelamentoV3(objPedCanc, cert));

                }
                else
                {
                    throw new Exception("Cadastro de Empresa não configurado para enviar NFe-serviço");
                }
                //string sMsg = ConfiguraMsgRetornoCancelamento(sRet);
                // return sMsg;
                return sRet;
            }
            catch (Exception ex)
            {
                throw;
            }
        }