private IFileInformation TryGenerateXMLFile(IFileInformation _FileInformation, String Destination, FileConversionUpdate _FileConversionUpdate)
        {
            String FileText = File.ReadAllText(_FileInformation.Path.LocalPath);

            if (!FileText.Contains("FatturaElettronicaHeader") && !FileText.Contains("FatturaElettronicaBody"))
            {
                throw new InvalidOperationException("Unsupported Format" + _FileInformation.Path);
            }


            int    _XMLStartIndex   = FileText.IndexOf(Conventions.HeaderGaurd) - 1;
            int    _XMLEndIndex     = FileText.LastIndexOf(Conventions.BodyGaurd) - _XMLStartIndex + Conventions.BodyGaurd.Length + 1;
            String _RawBoundedData  = FileText.Substring(_XMLStartIndex, _XMLEndIndex);
            String _PossibleXMLData = XML.Conventions.Header + Conventions.Header + CleanInvalidXmlChars(_RawBoundedData) + Conventions.Footer;

            FileInformation _ResultFileInformation = new FileInformation(new FileFormat(EFileExtension.XML, EFormat.Uknown, ""), Destination + "\\" + _FileInformation.FileName + ".xml");

            using (XmlReader _XmlReader = XmlReader.Create(new StringReader(_PossibleXMLData), new XmlReaderSettings {
                IgnoreWhitespace = true, IgnoreComments = true
            }))
            {
                Fattura _Fattura = new Fattura( );

                try
                {
                    _Fattura.ReadXml(_XmlReader);

                    if (_Fattura.Validate( ).IsValid)
                    {
                        using (XmlWriter _XmlWriter = XmlWriter.Create(_ResultFileInformation.Path.LocalPath, new XmlWriterSettings {
                            Indent = true
                        }))
                        {
                            _Fattura.WriteXml(_XmlWriter);
                        }

                        _FileConversionUpdate.AddTransformation(EFileTransformation.ConvertedToCopied, _FileInformation, _ResultFileInformation);
                    }
                    else
                    {
                        throw new ArgumentException("Invalid XMLPA FileContent ");
                    }
                }
                catch (Exception Ex)
                {
                    File.WriteAllText(_ResultFileInformation.Path.LocalPath, _PossibleXMLData);

                    _FileConversionUpdate.AddTransformation(EFileTransformation.ConvertedToCopied,
                                                            _FileInformation,
                                                            _ResultFileInformation,
                                                            new StateEventArgs(ESourceState.Unstable, Ex));
                }
            }


            return(File.Exists(_ResultFileInformation.Path.LocalPath) ? _ResultFileInformation : null);
        }
        private void ProcessFiles(String Destination)
        {
            while (State == EXPDFConverterState.Working && !_Aborting)
            {
                FileConversionUpdate _FileConversionUpdate = null;

                try
                {
                    _FileConversionUpdate = new FileConversionUpdate(_FileConversionQueue.GetNext( ));

                    _DocumentsToPrint.Add(_ConvertDocument(_FileConversionUpdate, Destination));

                    _InvokeFileProgressUpdateEvent(_FileConversionUpdate);
                }
                catch (Exception Ex)
                {
                    if (_FileConversionUpdate is null)
                    {
                        _FileConversionUpdate = new FileConversionUpdate(null);

                        _FileConversionUpdate.AddTransformation(new FileTransformation(EFileTransformation.Failed,
                                                                                       _FileConversionUpdate.Original,
                                                                                       null,
                                                                                       new StateEventArgs(ESourceState.Unstable, Ex)));
                    }

                    _InvokeFileProgressUpdateEvent(_FileConversionUpdate);
                }


                if (_FileConversionQueue.PercentItterated == 1)
                {
                    if (PrintDocuments)
                    {
                        Parallel.ForEach <String>(_DocumentsToPrint,
                                                  new Action <String, ParallelLoopState>((String _Document, ParallelLoopState state) =>
                        {
                            if (_Aborting)
                            {
                                state.Break( );
                            }
                            else
                            {
                                if (!String.IsNullOrEmpty(_Document))
                                {
                                    PDFPrinter.Print(_Document, Settings.Default.SelectedPrinter);
                                    Log.Commit("Sent To Printer:\t" + _Document);
                                }
                            }
                        }));

                        Log.Commit( );
                        Log.Commit( );
                    }

                    SetState(EXPDFConverterState.Available);
                }
            }
        }
        private String _ConvertDocument(FileConversionUpdate _FileConversionUpdate, String Destination)
        {
            IFileInformation ConvertedFileInfo = _FileConversionUpdate.Original;

            if (ConvertedFileInfo.FormatInformation.FileExtension != EFileExtension.XML)
            {
                IFileInformation _AutoXML = TryGenerateXMLFile(ConvertedFileInfo, Destination, _FileConversionUpdate);

                if (_AutoXML == null)
                {
                    return(null);
                }


                try
                {
                    if (!Settings.Default.EnablePDF)
                    {
                        return(null);
                    }

                    ConvertedFileInfo = _FEFileConverter.Convert(_AutoXML);

                    _FileConversionUpdate.AddTransformation(EFileTransformation.ConvertedToCopied, _AutoXML, ConvertedFileInfo);

                    if (ConvertedFileInfo == null)
                    {
                        return(null);
                    }
                }
                catch (Exception Ex)
                {
                    _FileConversionUpdate.AddTransformation(EFileTransformation.Failed,
                                                            _AutoXML,
                                                            null,
                                                            new StateEventArgs(ESourceState.Failed, Ex));

                    throw Ex;
                }
                finally
                {
                    if (!Settings.Default.EnableXML)
                    {
                        File.Delete(_AutoXML.Path.LocalPath);

                        _FileConversionUpdate.AddTransformation(EFileTransformation.Deleted, null);
                    }
                }
            }
            else
            {
                if (!Settings.Default.EnablePDF)
                {
                    return(null);
                }

                try
                {
                    ConvertedFileInfo = Convert(ConvertedFileInfo);

                    _FileConversionUpdate.AddTransformation(EFileTransformation.ConvertedToCopied, ConvertedFileInfo);
                }
                catch (Exception Ex)
                {
                    _FileConversionUpdate.AddTransformation(EFileTransformation.Failed,
                                                            ConvertedFileInfo,
                                                            new StateEventArgs(ESourceState.Failed, Ex));

                    throw Ex;
                }



                if (ConvertedFileInfo == null)
                {
                    return(null);
                }
            }

            if (ConvertedFileInfo != null)
            {
                if (!Settings.Default.InheritFileName)
                {
                    if (File.Exists(ConvertedFileInfo.FallbackPath))
                    {
                        File.Delete(ConvertedFileInfo.FallbackPath);
                    }

                    File.Move(ConvertedFileInfo.Path.LocalPath, ConvertedFileInfo.FallbackPath);

                    ConvertedFileInfo = new FileInformation(ConvertedFileInfo.FormatInformation, ConvertedFileInfo.FallbackPath);

                    _FileConversionUpdate.AddTransformation(EFileTransformation.RenamedTo, ConvertedFileInfo);
                }

                if (Destination != ConvertedFileInfo.Directory)
                {
                    if (File.Exists(Destination + "\\" + ConvertedFileInfo.FileName))
                    {
                        File.Delete(Destination + "\\" + ConvertedFileInfo.FileName);
                    }

                    File.Move(ConvertedFileInfo.Path.LocalPath, Destination + "\\" + ConvertedFileInfo.FileName);

                    _FileConversionUpdate.AddTransformation(EFileTransformation.RenamedTo, new FileInformation(ConvertedFileInfo.FormatInformation,
                                                                                                               Destination + "\\" + ConvertedFileInfo.FileName));

                    return(Destination + "\\" + ConvertedFileInfo.FileName);
                }

                return(ConvertedFileInfo.Path.LocalPath);
            }

            return(null);
        }