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); }
public bool IsValidXML(String InputXML) { using (var r = XmlReader.Create(new StringReader(InputXML), _XmlReaderSettings)) { _FatturaDocument = new Fattura( ); _FatturaDocument.ReadXml(r); return(_FatturaDocument.Validate( ).IsValid); } }
private bool ConvalidaForm() { errori.Items.Clear(); _result = _fattura.Validate(); if (_result.IsValid) { lblErrori.Text = "Nessun errore riscontrato."; salvaApri.Enabled = false; return(true); } foreach (var err in _result.Errors) { var item = new ListViewItem(err.PropertyName.Replace("FatturaElettronicaHeader.", "")); item.SubItems.Add(err.ErrorMessage); item.SubItems.Add(err.ErrorCode); errori.Items.Add(item); } errori.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); errori.Columns[1].Width = 300; errori.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent); lblErrori.Text = "Riscontrati alcuni errori di convalida."; salvaApri.Enabled = true; return(false); }