Ejemplo n.º 1
0
        public static ResAcc <string> TransformarXml(string sXml, string sXsl)
        {
            var Res = new ResAcc <string>(true);

            var                  oTexto   = new StringWriterMod(Encoding.UTF8);
            var                  oXml     = XmlWriter.Create(oTexto, FacturaXml.ConfigXml);
            XmlReader            oXsl     = XmlReader.Create(new StringReader(sXsl));
            XmlReader            oFactura = XmlReader.Create(new StringReader(sXml));
            XslCompiledTransform TransXsl = new XslCompiledTransform();
            string               sXmlRes  = "";

            try
            {
                TransXsl.Load(oXsl);
                TransXsl.Transform(oFactura, oXml);
                sXmlRes = oTexto.ToString();
            }
            catch (Exception e)
            {
                Res.Exito    = false;
                Res.Mensaje  = "Error en transformación de Xml\n\n";
                Res.Mensaje += (e.InnerException == null ? e.Message : e.InnerException.Message);
            }
            oXsl.Close();
            oFactura.Close();
            oXml.Close();
            oTexto.Close();

            Res.Respuesta = sXmlRes;
            return(Res);
        }
Ejemplo n.º 2
0
        public static ResAcc<string> GenerarXmlDeObjeto(object Objeto)
        {
            var oTexto = new StringWriterMod(Encoding.UTF8);
            var oXml = XmlWriter.Create(oTexto, FacturaXml.ConfigXml);
            var oSer = new XmlSerializer(Objeto.GetType());
            oSer.Serialize(oXml, Objeto);
            string sXml = oTexto.ToString();
            oXml.Close();
            oTexto.Close();

            return new ResAcc<string>(true) { Respuesta = sXml };
        }
Ejemplo n.º 3
0
        public static ResAcc<FacturaElectronica> GenerarXml(FacturaElectronica oFactura)
        {
            // Se genera el xml con los datos de la factura
            var ResProcs = FacturaXml.GenerarXmlDeObjeto(oFactura);
            string sFacturaXml = ResProcs.Respuesta;

            // Se transforma al Xml requerido por el Sat v3.2
            ResProcs = FacturaXml.TransformarXml(sFacturaXml, Properties.Resources.Cfdi3_2);
            if (ResProcs.Error)
                return new ResAcc<FacturaElectronica>(false, ResProcs.Mensaje);
            string sCfdiXml = ResProcs.Respuesta;

            // Se valida el Xml generado
            ResProcs = FacturaXml.ValidarXml(sCfdiXml, Properties.Resources.cfdv32);
            if (ResProcs.Error)
                return new ResAcc<FacturaElectronica>(false, ResProcs.Mensaje);

            // Se genera la cadena original
            ResProcs = FacturaXml.TransformarXml(sCfdiXml, Properties.Resources.cadenaoriginal_3_2);
            if (ResProcs.Error)
                return new ResAcc<FacturaElectronica>(false, ResProcs.Mensaje);
            string sCadenaOriginal = System.Web.HttpUtility.HtmlDecode(ResProcs.Respuesta);

            // Se genera la cadena del sello digital
            var Seguridad = new FacturaSeguridad();
            ResProcs = Seguridad.GenerarSelloDigital(oFactura.Configuracion.RutaArchivoKey, oFactura.Configuracion.ContraseniaArchivoKey, sCadenaOriginal);
            if (ResProcs.Error)
                return new ResAcc<FacturaElectronica>(false, ResProcs.Mensaje);
            string sSelloDigital = ResProcs.Respuesta;

            // Se agregan los nuevos datos al xml a generar
            var oCertificado = Seguridad.DatosCertificado(oFactura.Configuracion.RutaCertificado);
            XmlDocument oXml = new XmlDocument();
            oXml.LoadXml(sCfdiXml);
            oXml.DocumentElement.Attributes["noCertificado"].Value = oCertificado["NumeroDeSerie"];
            oXml.DocumentElement.Attributes["certificado"].Value = oCertificado["ContenidoBase64"];
            oXml.DocumentElement.Attributes["sello"].Value = sSelloDigital;
            // Se guarda el xml con los cambios
            var oTexto = new StringWriterMod(Encoding.UTF8);
            var oEscXml = XmlWriter.Create(oTexto, FacturaXml.ConfigXml);
            oXml.Save(oEscXml);
            ResProcs.Exito = true;
            ResProcs.Respuesta = oTexto.ToString();

            oFactura.NumeroDeCertificado = oCertificado["NumeroDeSerie"];
            oFactura.Certificado = oCertificado["ContenidoBase64"];
            oFactura.Sello = sSelloDigital;
            oFactura.XmlFactura = ResProcs.Respuesta;

            return new ResAcc<FacturaElectronica>(true, oFactura);
        }
Ejemplo n.º 4
0
        public static ResAcc <string> GenerarXmlDeObjeto(object Objeto)
        {
            var oTexto = new StringWriterMod(Encoding.UTF8);
            var oXml   = XmlWriter.Create(oTexto, FacturaXml.ConfigXml);
            var oSer   = new XmlSerializer(Objeto.GetType());

            oSer.Serialize(oXml, Objeto);
            string sXml = oTexto.ToString();

            oXml.Close();
            oTexto.Close();

            return(new ResAcc <string>(true)
            {
                Respuesta = sXml
            });
        }
Ejemplo n.º 5
0
        public static ResAcc<string> TransformarXml(string sXml, string sXsl)
        {
            var Res = new ResAcc<string>(true);

            var oTexto = new StringWriterMod(Encoding.UTF8);
            var oXml = XmlWriter.Create(oTexto, FacturaXml.ConfigXml);
            XmlReader oXsl = XmlReader.Create(new StringReader(sXsl));
            XmlReader oFactura = XmlReader.Create(new StringReader(sXml));
            XslCompiledTransform TransXsl = new XslCompiledTransform();
            string sXmlRes = "";
            try
            {
                TransXsl.Load(oXsl);
                TransXsl.Transform(oFactura, oXml);
                sXmlRes = oTexto.ToString();
            }
            catch (Exception e)
            {
                Res.Exito = false;
                Res.Mensaje = "Error en transformación de Xml\n\n";
                Res.Mensaje += (e.InnerException == null ? e.Message : e.InnerException.Message);
            }
            oXsl.Close();
            oFactura.Close();
            oXml.Close();
            oTexto.Close();

            Res.Respuesta = sXmlRes;
            return Res;
        }
Ejemplo n.º 6
0
        public static ResAcc <FacturaElectronica> GenerarXml(FacturaElectronica oFactura)
        {
            // Se genera el xml con los datos de la factura
            var    ResProcs    = FacturaXml.GenerarXmlDeObjeto(oFactura);
            string sFacturaXml = ResProcs.Respuesta;

            // Se transforma al Xml requerido por el Sat v3.2
            ResProcs = FacturaXml.TransformarXml(sFacturaXml, Properties.Resources.Cfdi3_2);
            if (ResProcs.Error)
            {
                return(new ResAcc <FacturaElectronica>(false, ResProcs.Mensaje));
            }
            string sCfdiXml = ResProcs.Respuesta;

            // Se valida el Xml generado
            ResProcs = FacturaXml.ValidarXml(sCfdiXml, Properties.Resources.cfdv32);
            if (ResProcs.Error)
            {
                return(new ResAcc <FacturaElectronica>(false, ResProcs.Mensaje));
            }

            // Se genera la cadena original
            ResProcs = FacturaXml.TransformarXml(sCfdiXml, Properties.Resources.cadenaoriginal_3_2);
            if (ResProcs.Error)
            {
                return(new ResAcc <FacturaElectronica>(false, ResProcs.Mensaje));
            }
            string sCadenaOriginal = System.Web.HttpUtility.HtmlDecode(ResProcs.Respuesta);

            // Se genera la cadena del sello digital
            var Seguridad = new FacturaSeguridad();

            ResProcs = Seguridad.GenerarSelloDigital(oFactura.Configuracion.RutaArchivoKey, oFactura.Configuracion.ContraseniaArchivoKey, sCadenaOriginal);
            if (ResProcs.Error)
            {
                return(new ResAcc <FacturaElectronica>(false, ResProcs.Mensaje));
            }
            string sSelloDigital = ResProcs.Respuesta;

            // Se agregan los nuevos datos al xml a generar
            var         oCertificado = Seguridad.DatosCertificado(oFactura.Configuracion.RutaCertificado);
            XmlDocument oXml         = new XmlDocument();

            oXml.LoadXml(sCfdiXml);
            oXml.DocumentElement.Attributes["noCertificado"].Value = oCertificado["NumeroDeSerie"];
            oXml.DocumentElement.Attributes["certificado"].Value   = oCertificado["ContenidoBase64"];
            oXml.DocumentElement.Attributes["sello"].Value         = sSelloDigital;
            // Se guarda el xml con los cambios
            var oTexto  = new StringWriterMod(Encoding.UTF8);
            var oEscXml = XmlWriter.Create(oTexto, FacturaXml.ConfigXml);

            oXml.Save(oEscXml);
            ResProcs.Exito     = true;
            ResProcs.Respuesta = oTexto.ToString();

            oFactura.NumeroDeCertificado = oCertificado["NumeroDeSerie"];
            oFactura.Certificado         = oCertificado["ContenidoBase64"];
            oFactura.Sello      = sSelloDigital;
            oFactura.XmlFactura = ResProcs.Respuesta;

            return(new ResAcc <FacturaElectronica>(true, oFactura));
        }