public static System.Xml.XmlDocument SetInfopathForm( System.Xml.XmlDocument part, string InfopathFormUrl) { // Delete existing processing instructions. foreach (System.Xml.XmlNode pi in part.SelectNodes( "processing-instruction()")) { pi.ParentNode.RemoveChild(pi); } // Add an xml declaration System.Xml.XmlDeclaration decl = part.CreateXmlDeclaration("1.0", null, null); part.InsertBefore(decl, part.DocumentElement); // Create the mso-application procesing instruction. System.Xml.XmlProcessingInstruction progid = part.CreateProcessingInstruction( "mso-application", "progid='InfoPath.Document'"); part.InsertBefore(progid, part.DocumentElement); // Create the mso-infoPathSolution processing instruction System.Xml.XmlProcessingInstruction form = part.CreateProcessingInstruction( "mso-infoPathSolution", "PIVersion='1.0.0.0' href='" + InfopathFormUrl + "'"); part.InsertBefore(form, part.DocumentElement); return(part); }
/// <summary> /// 创建一个新文档 /// </summary> /// <returns></returns> public static System.Xml.XmlDocument NewDocment() { var doc = new System.Xml.XmlDocument(); doc.AppendChild(doc.CreateProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"")); return(doc); }
public ActionResult ajxPreviewTemplate(string tempName) { InvTemplate it = new InvTemplate(); IInvTemplateService _invTempSrc = IoC.Resolve <IInvTemplateService>(); it = _invTempSrc.GetByName(tempName); System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument(); xdoc.PreserveWhitespace = true; xdoc.LoadXml(it.XmlFile); System.Xml.XmlProcessingInstruction newPI; String PItext = "type='text/xsl' href='" + FX.Utils.UrlUtil.GetSiteUrl() + "/InvoiceTemplate/GetXSLTbyTempName?tempname=" + it.TemplateName + "'"; newPI = xdoc.CreateProcessingInstruction("xml-stylesheet", PItext); xdoc.InsertBefore(newPI, xdoc.DocumentElement); logtest.Info("tempName: " + tempName + " href: " + PItext); //IViewer _iViewerSrv = IoC.Resolve<IViewer>(); IViewer _iViewerSrv = InvServiceFactory.GetViewer(tempName); return(Json(_iViewerSrv.GetHtml(System.Text.Encoding.UTF8.GetBytes(xdoc.OuterXml)))); }
private void DataExplorerNodeResultsGrid_ExportToExcel() { if (DataExplorerTreeView.CheckedNodes.Count == 0) { return; } Boolean usesEntityCurrentMailingAddress = false; Boolean usesEntityCurrentContactInformation = false; Boolean usesMemberCurrentEnrollment = false; Boolean usesMemberCurrentEnrollmentCoverage = false; Boolean usesMemberCurrentEnrollmentPcp = false; System.Xml.XmlElement styleElement; System.Xml.XmlElement fontElement; System.Xml.XmlElement column; System.Xml.XmlElement row; System.Xml.XmlElement cell; System.Xml.XmlElement cellData; Int32 columnIndex = 0; System.Xml.XmlDocument excelDocument = new System.Xml.XmlDocument(); System.Xml.XmlDeclaration xmlDeclaration = excelDocument.CreateXmlDeclaration("1.0", "utf-8", String.Empty); excelDocument.InsertBefore(xmlDeclaration, excelDocument.DocumentElement); excelDocument.AppendChild(excelDocument.CreateProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"")); #region Create Workbook and Worksheet and Styles System.Xml.XmlElement workbookElement = excelDocument.CreateElement("Workbook"); workbookElement.SetAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet"); workbookElement.SetAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office"); workbookElement.SetAttribute("xmlns:ss", "urn:schemas-microsoft-com:office:spreadsheet"); workbookElement.SetAttribute("xmlns:x", "urn:schemas-microsoft-com:office:excel"); excelDocument.AppendChild(workbookElement); System.Xml.XmlElement stylesCollectionElement = excelDocument.CreateElement("Styles"); workbookElement.AppendChild(stylesCollectionElement); styleElement = excelDocument.CreateElement("Style"); styleElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "ID", "urn:schemas-microsoft-com:office:spreadsheet", "StyleNormal")); stylesCollectionElement.AppendChild(styleElement); fontElement = excelDocument.CreateElement("Font"); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "FontName", "urn:schemas-microsoft-com:office:spreadsheet", "Arial")); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "x", "Family", "urn:schemas-microsoft-com:office:excel", "Swiss")); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Size", "urn:schemas-microsoft-com:office:spreadsheet", "8")); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Color", "urn:schemas-microsoft-com:office:spreadsheet", "#000000")); styleElement.AppendChild(fontElement); styleElement = excelDocument.CreateElement("Style"); styleElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "ID", "urn:schemas-microsoft-com:office:spreadsheet", "StyleBold")); stylesCollectionElement.AppendChild(styleElement); fontElement = excelDocument.CreateElement("Font"); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "FontName", "urn:schemas-microsoft-com:office:spreadsheet", "Arial")); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "x", "Family", "urn:schemas-microsoft-com:office:excel", "Swiss")); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Size", "urn:schemas-microsoft-com:office:spreadsheet", "8")); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Color", "urn:schemas-microsoft-com:office:spreadsheet", "#000000")); fontElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Bold", "urn:schemas-microsoft-com:office:spreadsheet", "1")); styleElement.AppendChild(fontElement); System.Xml.XmlElement worksheetElement = excelDocument.CreateElement("Worksheet"); worksheetElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Name", "urn:schemas-microsoft-com:office:spreadsheet", "Data")); workbookElement.AppendChild(worksheetElement); System.Xml.XmlElement worksheetTableElement = excelDocument.CreateElement("Table"); worksheetTableElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "StyleID", "urn:schemas-microsoft-com:office:spreadsheet", "StyleNormal")); worksheetTableElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "DefaultRowHeight", "urn:schemas-microsoft-com:office:spreadsheet", "15")); worksheetElement.AppendChild(worksheetTableElement); #endregion #region Create Header Row System.Xml.XmlElement headerRow = excelDocument.CreateElement("Row"); columnIndex = 0; foreach (Telerik.Web.UI.RadTreeNode currentCheckedNode in DataExplorerTreeView.CheckedNodes) { columnIndex = columnIndex + 1; column = excelDocument.CreateElement("Column"); column.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Width", "urn:schemas-microsoft-com:office:spreadsheet", "100")); column.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "StyleID", "urn:schemas-microsoft-com:office:spreadsheet", "StyleNormal")); worksheetTableElement.AppendChild(column); cell = excelDocument.CreateElement("Cell"); cell.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "StyleID", "urn:schemas-microsoft-com:office:spreadsheet", "StyleBold")); cell.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Index", "urn:schemas-microsoft-com:office:spreadsheet", columnIndex.ToString())); headerRow.AppendChild(cell); cellData = excelDocument.CreateElement("Data"); cellData.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Type", "urn:schemas-microsoft-com:office:spreadsheet", "String")); cellData.InnerText = currentCheckedNode.Text; cell.AppendChild(cellData); usesEntityCurrentMailingAddress |= currentCheckedNode.Value.StartsWith("Entity.CurrentMailingAddress."); usesEntityCurrentContactInformation |= currentCheckedNode.Value.StartsWith("Entity.CurrentContactInformation"); usesMemberCurrentEnrollment |= currentCheckedNode.Value.StartsWith("CurrentEnrollment."); usesMemberCurrentEnrollmentCoverage |= currentCheckedNode.Value.StartsWith("CurrentEnrollmentCoverage"); usesMemberCurrentEnrollmentPcp |= currentCheckedNode.Value.StartsWith("CurrentEnrollmentPcp"); } worksheetTableElement.AppendChild(headerRow); #endregion #region Create Data Rows // RETREIVE LIST OF MEMBERS AND PRE-CACHE RELATED DATA List <Client.Core.Member.Member> members = MercuryApplication.DataExplorerNodeResultsGetForMember(NodeInstanceId, 1, NodeInstanceCount); List <Client.Core.Member.MemberEnrollment> allMemberEnrollments = null; List <Client.Core.Member.MemberEnrollmentCoverage> allMemberEnrollmentCoverages = null; List <Client.Core.Member.MemberEnrollmentPcp> allMemberEnrollmentPcps = null; // THESE ITEMS ARE AUTOMATICALLY CACHED AND AVAILABLE if (usesEntityCurrentMailingAddress) { MercuryApplication.DataExplorerNodeResultsGetForMemberEntityCurrentAddress(NodeInstanceId, 1, NodeInstanceCount); } if (usesEntityCurrentContactInformation) { MercuryApplication.DataExplorerNodeResultsGetForMemberEntityCurrentContactInformation(NodeInstanceId, 1, NodeInstanceCount); } // THESE ITEMS MUST BE STORED AND USED LOCALLY (COULD BE LARG RESULT SETS!) if ((usesMemberCurrentEnrollment) || (usesMemberCurrentEnrollmentCoverage) || (usesMemberCurrentEnrollmentPcp)) { // MUST GET CURRENT ENROLLMENTS TO WALK TO CHILD OBJECTS COVERAGE AND PCP allMemberEnrollments = MercuryApplication.DataExplorerNodeResultsGetForMemberCurrentEnrollment(NodeInstanceId, 1, NodeInstanceCount); if (usesMemberCurrentEnrollmentCoverage) { allMemberEnrollmentCoverages = MercuryApplication.DataExplorerNodeResultsGetForMemberCurrentEnrollmentCoverage(NodeInstanceId, 1, NodeInstanceCount); } if (usesMemberCurrentEnrollmentPcp) { allMemberEnrollmentPcps = MercuryApplication.DataExplorerNodeResultsGetForMemberCurrentEnrollmentPcp(NodeInstanceId, 1, NodeInstanceCount); } } foreach (Client.Core.Member.Member currentMember in members) { row = excelDocument.CreateElement("Row"); columnIndex = 0; #region Precache Data Elements // PRECACHE CURRENT MEMBER ENROLLMENT FOR MULTI-PROPERTY ACCESS // MUST GET CURRENT ENROLLMENTS TO WALK TO CHILD OBJECTS COVERAGE AND PCP Client.Core.Member.MemberEnrollment currentMemberEnrollment = null; if ((usesMemberCurrentEnrollment) || (usesMemberCurrentEnrollmentCoverage) || (usesMemberCurrentEnrollmentPcp)) { List <Client.Core.Member.MemberEnrollment> filteredMemberEnrollment = (from memberEnrollment in allMemberEnrollments where memberEnrollment.MemberId == currentMember.Id select memberEnrollment).ToList(); if (filteredMemberEnrollment.Count > 0) { currentMemberEnrollment = filteredMemberEnrollment[0]; } } // PRECACHE CURRENT MEMBER ENROLLMENT COVERAGE FOR MULTI-PROPERTY ACCESS Client.Core.Member.MemberEnrollmentCoverage currentMemberEnrollmentCoverage = null; if ((usesMemberCurrentEnrollmentCoverage) && (currentMemberEnrollment != null)) { List <Client.Core.Member.MemberEnrollmentCoverage> filteredMemberEnrollmentCoverage = (from memberEnrollmentCoverage in allMemberEnrollmentCoverages where memberEnrollmentCoverage.MemberEnrollmentId == currentMemberEnrollment.Id select memberEnrollmentCoverage).ToList(); if (filteredMemberEnrollmentCoverage.Count > 0) { currentMemberEnrollmentCoverage = filteredMemberEnrollmentCoverage[0]; } } Client.Core.Member.MemberEnrollmentPcp currentMemberEnrollmentPcp = null; if ((usesMemberCurrentEnrollmentPcp) && (currentMemberEnrollment != null)) { List <Client.Core.Member.MemberEnrollmentPcp> filteredMemberEnrollmentPcp = (from memberEnrollmentPcp in allMemberEnrollmentPcps where memberEnrollmentPcp.MemberEnrollmentId == currentMemberEnrollment.Id select memberEnrollmentPcp).ToList(); if (filteredMemberEnrollmentPcp.Count > 0) { currentMemberEnrollmentPcp = filteredMemberEnrollmentPcp[0]; } } #endregion #region Create Cells and Data Values foreach (Telerik.Web.UI.RadTreeNode currentCheckedNode in DataExplorerTreeView.CheckedNodes) { columnIndex = columnIndex + 1; String contentProperty = String.Empty; Object contentValueObject = null; String contentValue = String.Empty; if (currentCheckedNode.Value.StartsWith("CurrentEnrollment.")) { if (currentMemberEnrollment != null) { contentProperty = currentCheckedNode.Value.Substring("CurrentEnrollment.".Length, currentCheckedNode.Value.Length - "CurrentEnrollment.".Length); contentValueObject = Mercury.Server.CommonFunctions.GetPropertyValue(currentMemberEnrollment, contentProperty); } } else if (currentCheckedNode.Value.StartsWith("CurrentEnrollmentCoverage.")) { if (currentMemberEnrollment != null) { contentProperty = currentCheckedNode.Value.Substring("CurrentEnrollmentCoverage.".Length, currentCheckedNode.Value.Length - "CurrentEnrollmentCoverage.".Length); contentValueObject = Mercury.Server.CommonFunctions.GetPropertyValue(currentMemberEnrollmentCoverage, contentProperty); } } else if (currentCheckedNode.Value.StartsWith("CurrentEnrollmentPcp.")) { if (currentMemberEnrollment != null) { contentProperty = currentCheckedNode.Value.Substring("CurrentEnrollmentPcp.".Length, currentCheckedNode.Value.Length - "CurrentEnrollmentPcp.".Length); contentValueObject = Mercury.Server.CommonFunctions.GetPropertyValue(currentMemberEnrollmentPcp, contentProperty); } } else { contentValueObject = Mercury.Server.CommonFunctions.GetPropertyValue(currentMember, currentCheckedNode.Value); } if (contentValueObject != null) { contentValue = contentValueObject.ToString(); } cell = excelDocument.CreateElement("Cell"); cell.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Index", "urn:schemas-microsoft-com:office:spreadsheet", columnIndex.ToString())); row.AppendChild(cell); cellData = excelDocument.CreateElement("Data"); cellData.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "ss", "Type", "urn:schemas-microsoft-com:office:spreadsheet", "String")); cellData.InnerText = contentValue; cell.AppendChild(cellData); } #endregion worksheetTableElement.AppendChild(row); } #endregion System.Xml.XmlElement worksheetAutoFilterElement = excelDocument.CreateElement("AutoFilter"); worksheetAutoFilterElement.SetAttributeNode(XmlDocument_CreateAttribute(excelDocument, "x", "Range", "urn:schemas-microsoft-com:office:excel", "R1C1:R1C1")); worksheetAutoFilterElement.SetAttribute("xmlns", "urn:schemas-microsoft-com:office:excel"); worksheetElement.AppendChild(worksheetAutoFilterElement); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=DataExplorerResults.xml"); Response.AddHeader("Content-Length", excelDocument.OuterXml.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.OutputStream.Write(new System.Text.ASCIIEncoding().GetBytes(excelDocument.OuterXml.ToCharArray()), 0, excelDocument.OuterXml.Length); Response.End(); return; }
/// <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); }
/// <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); }
/// <summary> /// Create and Save a default configuration Xml file based on a easily specific or default defined xml string /// </summary> /// <param name="FilePath">The fullpath including filename.xml to save to</param> /// <param name="XmlConfigurationFileString">If not specified, then will use the XmlDefaultConfigurationFileString</param> /// <returns>True if succesfull created</returns> /// <remarks></remarks> public bool SaveXmlDefaultConfigurationFile(string FilePath, string XmlConfigurationFileString) { string theXmlString = ((XmlConfigurationFileString != null) ? XmlConfigurationFileString : XmlDefaultConfigurationFileString); //What Method You want to use ?, try any by simply change the var XmlSaveMethod declared at top switch (XmlSaveMethod) { case enumXmlSaveMethod.StreamWrite: //Easy Method using (System.IO.StreamWriter StreamWriter = new System.IO.StreamWriter(FilePath)) { //Without Compact Framework more easily using file.WriteAllText but, CF doesn't have this method StreamWriter.Write(theXmlString); StreamWriter.Close(); } return true; case enumXmlSaveMethod.XmlTextWriter: //Alternative Method using (System.Xml.XmlTextWriter XmlTextWriter = new System.Xml.XmlTextWriter(FilePath, System.Text.UTF8Encoding.UTF8)) { XmlTextWriter.WriteStartDocument(); XmlTextWriter.WriteStartElement("configuration"); XmlTextWriter.WriteStartElement("appSettings"); foreach (string Item in GetListItems(theXmlString)) { XmlTextWriter.WriteStartElement("add"); XmlTextWriter.WriteStartAttribute("key", string.Empty); XmlTextWriter.WriteRaw(GetKey(Item)); XmlTextWriter.WriteEndAttribute(); XmlTextWriter.WriteStartAttribute("value", string.Empty); XmlTextWriter.WriteRaw(GetValue(Item)); XmlTextWriter.WriteEndAttribute(); XmlTextWriter.WriteEndElement(); } XmlTextWriter.WriteEndElement(); XmlTextWriter.WriteEndElement(); //XmlTextWriter.WriteEndDocument() XmlTextWriter.Close(); } return true; case enumXmlSaveMethod.XmlDocument: //Method you will practice System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument(); System.Xml.XmlElement xRoot = XmlDoc.CreateElement("configuration"); XmlDoc.AppendChild(xRoot); System.Xml.XmlElement xAppSettingsElement = XmlDoc.CreateElement("appSettings"); xRoot.AppendChild(xAppSettingsElement); System.Xml.XmlElement xElement = default(System.Xml.XmlElement); System.Xml.XmlAttribute xAttrKey = default(System.Xml.XmlAttribute); System.Xml.XmlAttribute xAttrValue = default(System.Xml.XmlAttribute); foreach (string Item in GetListItems(theXmlString)) { xElement = XmlDoc.CreateElement("add"); xAttrKey = XmlDoc.CreateAttribute("key"); xAttrValue = XmlDoc.CreateAttribute("value"); xAttrKey.InnerText = GetKey(Item); xElement.SetAttributeNode(xAttrKey); xAttrValue.InnerText = GetValue(Item); xElement.SetAttributeNode(xAttrValue); xAppSettingsElement.AppendChild(xElement); } System.Xml.XmlProcessingInstruction XmlPI = XmlDoc.CreateProcessingInstruction("xml", "version='1.0' encoding='utf-8'"); XmlDoc.InsertBefore(XmlPI, XmlDoc.ChildNodes[0]); XmlDoc.Save(FilePath); return true; } return false; }
public object SaveXmlCurrentConfiguration(string StrPath, string StrFilename /*= "AppSettings.xml"*/) { //Conform the Path string FilePath = (!(StrPath == null) ? StrPath : System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\" + StrFilename; System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument(); System.Xml.XmlElement xRoot = XmlDoc.CreateElement("configuration"); XmlDoc.AppendChild(xRoot); System.Xml.XmlElement xAppSettingsElement = XmlDoc.CreateElement("appSettings"); xRoot.AppendChild(xAppSettingsElement); System.Xml.XmlElement xElement = default(System.Xml.XmlElement); System.Xml.XmlAttribute xAttrKey = default(System.Xml.XmlAttribute); System.Xml.XmlAttribute xAttrValue = default(System.Xml.XmlAttribute); //For Each Item As String In Me.ListItems for (int i = 0; i <= this.ListItems.Count - 1; i++) { xElement = XmlDoc.CreateElement("add"); xAttrKey = XmlDoc.CreateAttribute("key"); xAttrValue = XmlDoc.CreateAttribute("value"); xAttrKey.InnerText = this.ListItems.GetKey(i); xElement.SetAttributeNode(xAttrKey); xAttrValue.InnerText = this.ListItems[i]; xElement.SetAttributeNode(xAttrValue); xAppSettingsElement.AppendChild(xElement); } System.Xml.XmlProcessingInstruction XmlPI = XmlDoc.CreateProcessingInstruction("xml", "version='1.0' encoding='utf-8'"); XmlDoc.InsertBefore(XmlPI, XmlDoc.ChildNodes[0]); XmlDoc.Save(FilePath); return true; }