Ejemplo n.º 1
0
        /// <summary>
        /// Modifica l'header del file XML della fatturazione elettronica con
        /// l'intestazione per il xlst in modo da essere visualizzato in modo umano
        /// Faillace 23/6/2014
        /// </summary>
        /// <param name="xmlByteArray">xml in ingresso</param>
        /// <returns>xml in uscita, se non è di tipo fatturazione , restituisce null</returns>
        private byte[] fatturazioneElettronicaInsertXlst(byte[] xmlByteArray)
        {
            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
            System.IO.TextReader   tr = new System.IO.StreamReader(new System.IO.MemoryStream(xmlByteArray));
            string fattura            = tr.ReadToEnd();

            fattura = fattura.Replace("<?xml version=\"1.1", "<?xml version=\"1.0"); //FIX per l'xml 1.1 (che non viene processato da dotnet)
            try
            {
                xd.LoadXml(fattura);
                //controllo se il namespace è tipo fattura elettronica
                if (xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/fatturapa/v1.0") ||
                    xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/fatturapa/v1.1"))
                {
                    System.Xml.XmlNamespaceManager mgr = new System.Xml.XmlNamespaceManager(xd.NameTable);
                    //se lo è aggiungo il namespace e il prefix
                    mgr.AddNamespace(xd.DocumentElement.Prefix, xd.DocumentElement.NamespaceURI);
                    //faccio una ricerca xpat per trovare il nodo FatturaElettronica
                    // Old Code
                    //System.Xml.XmlNode xn = xd.SelectSingleNode("//p:FatturaElettronica", mgr);
                    // New Code
                    System.Xml.XmlNode xn = null;

                    if (!string.IsNullOrEmpty(xd.DocumentElement.Prefix))
                    {
                        xn = xd.SelectSingleNode("//" + xd.DocumentElement.Prefix + ":FatturaElettronica", mgr);
                    }
                    else
                    {
                        //xn = xd.SelectSingleNode("FatturaElettronica");
                        if (xd.DocumentElement != null &&
                            !string.IsNullOrEmpty(xd.DocumentElement.Name) &&
                            xd.DocumentElement.Name.Equals("FatturaElettronica"))
                        {
                            xn = xd.DocumentElement;
                        }
                        else
                        {
                            xn = xd.SelectSingleNode("FatturaElettronica");
                        }
                    }

                    if (xn != null)
                    {
                        string nsUri = xd.DocumentElement.NamespaceURI.ToLower();
                        //Se è una fattura elettronica creo l'intestazione xlst con in path il file di trasfomazione che sta nell'xml di DPA
                        System.Xml.XmlProcessingInstruction dpaProcessingInformation = xd.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"../xml/fatturapa_v1.0.xsl\"");
                        if (nsUri.EndsWith("v1.1"))
                        {
                            dpaProcessingInformation = xd.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"../xml/fatturapa_v1.1.xsl\"");
                        }

                        //Ricerco il un eventuale xlst preessitente nel file xml
                        System.Xml.XmlProcessingInstruction foundProcessingInformation = xd.SelectSingleNode("//processing-instruction(\"xml-stylesheet\")") as System.Xml.XmlProcessingInstruction;
                        if (foundProcessingInformation != null)
                        {
                            // se lo trova lo rimpazza con quello geenrato sopra
                            xd.ReplaceChild(dpaProcessingInformation, foundProcessingInformation);
                        }
                        else
                        {
                            //inserisce l'xlst in cima al documento prima del primo tag
                            xd.InsertBefore(dpaProcessingInformation, xn);
                        }
                        //preparo un memorystream per l'output
                        System.IO.MemoryStream msOut = new System.IO.MemoryStream();
                        //salvo il risultato sul memorystream
                        xd.Save(msOut);
                        //esco con il risultato
                        return(msOut.ToArray());
                    }
                }
            }
            // il caricamento dell'xml ha dato errore (xml non formattato, tag non chiusi etc etc etc)
            catch
            { }
            //l'eleaborazione è avvenuta in modo corretto, o non è una fattura  elettronica
            //oppure non è un xml oppure si è verificato un errore
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Modifica l'header del file XML della fatturazione elettronica con
        /// l'intestazione per il xlst in modo da essere visualizzato in modo umano
        /// Faillace 23/6/2014
        /// </summary>
        /// <param name="xmlByteArray">xml in ingresso</param>
        /// <returns>xml in uscita, se non è di tipo fatturazione , restituisce null</returns>
        private byte[] fatturazioneElettronicaInsertXlst(byte[] xmlByteArray)
        {
            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
            //Predisposizione FIX xml 1.1 se escono anomalie decommenta il codice sotto e sostituisci la xd.Load con la xd.LoadXml nel try.

            System.IO.TextReader tr = new System.IO.StreamReader(new System.IO.MemoryStream(xmlByteArray));
            string fattura          = tr.ReadToEnd();

            fattura = fattura.Replace("<?xml version=\"1.1", "<?xml version=\"1.0");  //FIX per l'xml 1.1 (che non viene processato da dotnet)
            bool isFattura = false;

            try
            {
                xd.LoadXml(fattura);
                //xd.Load(new System.IO.MemoryStream(xmlByteArray));
                //controllo se il namespace è tipo fattura elettronica
                if (xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/fatturapa/v1.0") ||
                    xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/fatturapa/v1.1") ||
                    xd.DocumentElement.NamespaceURI.ToLower().Equals("http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2"))
                {
                    System.Xml.XmlNamespaceManager mgr = new System.Xml.XmlNamespaceManager(xd.NameTable);
                    //se lo è aggiungo il namespace e il prefix
                    mgr.AddNamespace(xd.DocumentElement.Prefix, xd.DocumentElement.NamespaceURI);
                    //faccio una ricerca xpat per trovare il nodo FatturaElettronica
                    // Old Code
                    //System.Xml.XmlNode xn = xd.SelectSingleNode("//p:FatturaElettronica", mgr);
                    // New Code
                    System.Xml.XmlNode xn = null;

                    if (!string.IsNullOrEmpty(xd.DocumentElement.Prefix))
                    {
                        xn = xd.SelectSingleNode("//" + xd.DocumentElement.Prefix + ":FatturaElettronica", mgr);
                    }
                    else
                    {
                        //xn = xd.SelectSingleNode("FatturaElettronica");
                        if (xd.DocumentElement != null &&
                            !string.IsNullOrEmpty(xd.DocumentElement.Name) &&
                            xd.DocumentElement.Name.Equals("FatturaElettronica"))
                        {
                            xn = xd.DocumentElement;
                        }
                        else
                        {
                            xn = xd.SelectSingleNode("FatturaElettronica");
                        }
                    }

                    if (xn != null)
                    {
                        string nsUri = xd.DocumentElement.NamespaceURI.ToLower();
                        //Se è una fattura elettronica creo l'intestazione xlst con in path il file di trasfomazione che sta nell'xml di DPA
                        System.Xml.XmlProcessingInstruction dpaProcessingInformation = xd.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"../xml/fatturapa_v1.0.xsl\"");
                        if (nsUri.EndsWith("v1.1"))
                        {
                            dpaProcessingInformation = xd.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"../xml/fatturapa_v1.1.xsl\"");
                        }
                        else if (nsUri.EndsWith("v1.2"))
                        {
                            dpaProcessingInformation = xd.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"../xml/fatturapa_v1.2.xsl\"");
                        }

                        //Ricerco il un eventuale xlst preessitente nel file xml
                        System.Xml.XmlProcessingInstruction foundProcessingInformation = xd.SelectSingleNode("//processing-instruction(\"xml-stylesheet\")") as System.Xml.XmlProcessingInstruction;
                        if (foundProcessingInformation != null)
                        {
                            // se lo trova lo rimpazza con quello geenrato sopra
                            xd.ReplaceChild(dpaProcessingInformation, foundProcessingInformation);
                        }
                        else
                        {
                            //inserisce l'xlst in cima al documento prima del primo tag
                            xd.InsertBefore(dpaProcessingInformation, xn);
                        }
                        //preparo un memorystream per l'output
                        System.IO.MemoryStream msOut = new System.IO.MemoryStream();
                        //salvo il risultato sul memorystream
                        xd.Save(msOut);
                        isFattura = true;
                        //esco con il risultato
                        return(msOut.ToArray());
                    }
                }
                if (!isFattura && (xd.DocumentElement.NamespaceURI.ToLower().Equals("http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fattura/messaggi/v1.0") ||
                                   xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/fatturapa/v1.0") ||
                                   xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/messaggi/v1.0")))
                {
                    // Ricevute SDI
                    System.Xml.XmlNamespaceManager mgr = new System.Xml.XmlNamespaceManager(xd.NameTable);

                    if (xd.DocumentElement.NamespaceURI.ToLower().Equals("http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fattura/messaggi/v1.0"))
                    {
                        mgr.AddNamespace("ns3", "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fattura/messaggi/v1.0");
                    }
                    else if (xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/fatturapa/v1.0"))
                    {
                        mgr.AddNamespace("ns3", "http://www.fatturapa.gov.it/sdi/fatturapa/v1.0");
                    }
                    else if (xd.DocumentElement.NamespaceURI.ToLower().Equals("http://www.fatturapa.gov.it/sdi/messaggi/v1.0"))
                    {
                        mgr.AddNamespace("ns3", "http://www.fatturapa.gov.it/sdi/messaggi/v1.0");
                    }
                    else
                    {
                        return(null);
                    }

                    string urlXSL = string.Empty;

                    if (xd.SelectSingleNode("//ns3:RicevutaConsegna", mgr) != null)
                    {
                        urlXSL = "../xml/RC_v1.1.xsl";
                    }
                    else if (xd.SelectSingleNode("//ns3:NotificaMancataConsegna", mgr) != null || xd.SelectSingleNode("//ns3:RicevutaImpossibilitaRecapito", mgr) != null)
                    {
                        urlXSL = "../xml/MC_v1.1.xsl";
                    }
                    else if (xd.SelectSingleNode("//ns3:NotificaScarto", mgr) != null || xd.SelectSingleNode("//ns3:RicevutaScarto", mgr) != null)
                    {
                        urlXSL = "../xml/NS_v1.1.xsl";
                    }
                    else if (xd.SelectSingleNode("//ns3:NotificaEsito", mgr) != null)
                    {
                        urlXSL = "../xml/EC_v1.0.xsl";
                    }
                    else if (xd.SelectSingleNode("//ns3:NotificaDecorrenzaTermini", mgr) != null)
                    {
                        urlXSL = "../xml/DT_v1.0.xsl";
                    }
                    else if (xd.SelectSingleNode("//types:ScartoEsitoCommittente", mgr) != null)
                    {
                        urlXSL = "../xml/SE_v1.0.xsl";
                    }


                    string decl  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    string decl8 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                    string pi    = "<?xml-stylesheet type=\"text/xsl\" href=\"" + urlXSL + "\"?>";

                    string previewXml = fattura.Replace(decl, decl + "\n" + pi);
                    previewXml = previewXml.Replace(decl8, decl8 + "\n" + pi);
                    xd.LoadXml(previewXml);
                    Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xd.OuterXml);

                    return(bytes);
                }
            }
            catch (Exception)
            {
            }

            return(null);
        }