private void SerializeAndAssertRootElementAttributes(FatturaBase f) { using (var w = XmlWriter.Create("test", new XmlWriterSettings { Indent = true })) { f.WriteXml(w); } using (var r = XmlReader.Create("test")) { while (r.Read()) { if (r.NodeType == XmlNodeType.Element) { if (r.Prefix == RootElement.Prefix && r.LocalName == "FatturaElettronicaSemplificata") { Assert.AreEqual(((FatturaSemplificata)f).FatturaElettronicaHeader.DatiTrasmissione.FormatoTrasmissione, r.GetAttribute("versione")); Assert.AreEqual("http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.0", r.NamespaceURI); foreach (var a in RootElement.ExtraAttributes) { Assert.AreEqual(a.value, r.GetAttribute(string.Format("{0}:{1}", a.Prefix, a.LocalName))); } break; } } } } File.Delete("test"); }
public static void WriteHtml(this FatturaBase fattura, string outPath, string xslPath) { if (outPath == null) { throw new ArgumentNullException(nameof(outPath)); } if (xslPath == null) { throw new ArgumentNullException(nameof(xslPath)); } var tempXml = Path.GetTempFileName(); using (var w = XmlWriter.Create(tempXml, new XmlWriterSettings { Indent = true })) { fattura.WriteXml(w); } var xt = new XslCompiledTransform(); xt.Load(xslPath); xt.Transform(tempXml, outPath); }
public static void WriteXmlSigned(this FatturaBase fattura, X509Certificate2 cert, string p7mFilePath) { string res = string.Empty; string tempFile = Path.GetTempFileName(); if (!p7mFilePath.ToLowerInvariant().EndsWith(".p7m")) { p7mFilePath += ".p7m"; } try { fattura.WriteXml(tempFile); ContentInfo content = new ContentInfo(new Oid("1.2.840.113549.1.7.1", "PKCS 7 Data"), File.ReadAllBytes(tempFile)); SignedCms signedCms = new SignedCms(SubjectIdentifierType.IssuerAndSerialNumber, content, false); CmsSigner signer = new CmsSigner(cert); signer.IncludeOption = X509IncludeOption.EndCertOnly; signer.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1", "SHA256"); signer.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.Now)); try { //PKCS7 format signedCms.ComputeSignature(signer, false); } catch (CryptographicException cex) { //To evaluate for the future https://stackoverflow.com/a/52897100 /* * // Try re-importing the private key into a better CSP: * using (RSA tmpRsa = RSA.Create()) * { * tmpRsa.ImportParameters(cert.GetRSAPrivateKey().ExportParameters(true)); * * using (X509Certificate2 tmpCertNoKey = new X509Certificate2(cert.RawData)) * using (X509Certificate2 tmpCert = tmpCertNoKey.CopyWithPrivateKey(tmpRsa)) * { * signer.Certificate = tmpCert; * signedCms.ComputeSignature(signer, false); * } * }*/ throw cex; } byte[] signature = signedCms.Encode(); File.WriteAllBytes(p7mFilePath, signature); } catch (Exception) { throw new FatturaElettronicaSignatureException(Resources.ErrorMessages.FirmaException); } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } } }
public static void WriteXml(this FatturaBase fattura, string filePath) { using (var w = XmlWriter.Create(filePath, new XmlWriterSettings { Indent = true })) { fattura.WriteXml(w); } }
private void SerializeAndAssertRootElementAttributes(FatturaBase f) { using (var w = XmlWriter.Create("test", new XmlWriterSettings { Indent = true })) { f.WriteXml(w); } using (var r = XmlReader.Create("test")) { while (r.Read()) { if (r.NodeType != XmlNodeType.Element) { continue; } if (r.Prefix != RootElement.Prefix || r.LocalName != "FatturaElettronica") { continue; } Assert.AreEqual( ((FatturaOrdinaria)f).FatturaElettronicaHeader.DatiTrasmissione.FormatoTrasmissione, r.GetAttribute("versione")); Assert.AreEqual(f.SistemaEmittente, r.GetAttribute("SistemaEmittente")); Assert.AreEqual("http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2", r.NamespaceURI); foreach (var a in RootElement.ExtraAttributes) { Assert.AreEqual(a.value, r.GetAttribute($"{a.Prefix}:{a.LocalName}")); } break; } } File.Delete("test"); }
public static void WriteHtml(this FatturaBase fattura, string outPath, string xslPath) { if (outPath == null) { throw new ArgumentNullException(nameof(outPath)); } if (xslPath == null) { throw new ArgumentNullException(nameof(xslPath)); } var xt = new XslCompiledTransform(); xt.Load(xslPath); using (var memoryStream = new MemoryStream()) { using (var w = new XmlTextWriter(memoryStream, Encoding.UTF8) { Formatting = Formatting.Indented }) { fattura.WriteXml(w); w.Flush(); memoryStream.Seek(0, SeekOrigin.Begin); var xPathDocument = new XPathDocument(memoryStream); using (var writer = File.OpenWrite(outPath)) { xt.Transform(xPathDocument, null, writer); } } } }
public static void WriteXml(this FatturaBase fattura, string filePath) { using var w = XmlWriter.Create(new StringBuilder(filePath), new XmlWriterSettings { Indent = true }); fattura.WriteXml(w); }
public static void WriteXml(this FatturaBase fattura, string filePath) { using var w = XmlWriter.Create(filePath, new() { Indent = true }); fattura.WriteXml(w); }