Example #1
0
        /// <summary>
        /// Apply an XSLT transform on a file to produce a resulting file
        /// </summary>
        /// <param name="sTransformName">full path name of the XSLT transform</param>
        /// <param name="parameterList">list of parameters to pass to the transform</param>
        /// <param name="sInputPath">full path of the input file</param>
        /// <param name="sOutputName">full path of the resulting output file</param>
        public static void TransformFileToFile(string sTransformName, XSLParameter[] parameterList, string sInputPath, string sOutputName)
        {
#if DEBUG
            Debug.WriteLine("Transform: " + sTransformName + " input file: " + sInputPath);
            DateTime start = DateTime.Now;
            Debug.WriteLine("\tStarting at: " + start.TimeOfDay.ToString());
#endif
#if UsingDotNetTransforms
            TextWriter writer = null;
            try
            {
                // set up transform
                XslCompiledTransform transformer = new XslCompiledTransform();
                transformer.Load(sTransformName);

                // add any parameters
                XsltArgumentList args;
                AddParameters(out args, parameterList);

                // setup output file
                writer = File.CreateText(sOutputName);

                // load input file
                XmlTextReader reader = new XmlTextReader(sInputPath);
                reader.ProhibitDtd    = false;
                reader.EntityHandling = EntityHandling.ExpandEntities;

                // Apply transform
                transformer.Transform(reader, args, writer);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
#else // not UsingDotNetTransforms
            //.Net framework XML transform is still slower than something like MSXML2
            // (this is so especially for transforms using xsl:key).
            MSXML2.XSLTemplate60Class             xslt   = new MSXML2.XSLTemplate60Class();
            MSXML2.FreeThreadedDOMDocument60Class xslDoc = new
                                                           MSXML2.FreeThreadedDOMDocument60Class();
            MSXML2.DOMDocument60Class xmlDoc = new MSXML2.DOMDocument60Class();
            MSXML2.IXSLProcessor      xslProc;

            xslDoc.async = false;
            xslDoc.setProperty("ResolveExternals", true);
            xslDoc.setProperty("ProhibitDTD", false);
            xslDoc.load(sTransformName);
            xslt.stylesheet = xslDoc;
            xmlDoc.setProperty("ResolveExternals", true);
            xmlDoc.setProperty("ProhibitDTD", false);
            xmlDoc.async = false;
            xmlDoc.load(sInputPath);
            xslProc       = xslt.createProcessor();
            xslProc.input = xmlDoc;
            AddParameters(parameterList, xslProc);
            xslProc.transform();
            StreamWriter sr = File.CreateText(sOutputName);
            sr.Write(xslProc.output);
            sr.Close();
#endif // UsingDotNetTransforms
#if DEBUG
            DateTime end = DateTime.Now;
            Debug.WriteLine("\tEnding at: " + end.TimeOfDay.ToString());
            System.TimeSpan diff = end.Subtract(start);
            Debug.WriteLine("\tProcess took: " + diff.ToString() + " " + sOutputName);
#endif
        }
Example #2
0
		/// <summary>
		/// Apply an XSLT transform on a file to produce a resulting file
		/// </summary>
		/// <param name="sTransformName">full path name of the XSLT transform</param>
		/// <param name="parameterList">list of parameters to pass to the transform</param>
		/// <param name="sInputPath">full path of the input file</param>
		/// <param name="sOutputName">full path of the resulting output file</param>
		public static void TransformFileToFile(string sTransformName, XSLParameter[] parameterList, string sInputPath, string sOutputName)
		{
#if DEBUG
			Debug.WriteLine("Transform: " + sTransformName + " input file: " + sInputPath);
			DateTime start = DateTime.Now;
			Debug.WriteLine("\tStarting at: " + start.TimeOfDay.ToString());
#endif
#if UsingDotNetTransforms
			TextWriter writer = null;
			try
			{
				// set up transform
				XslCompiledTransform transformer = new XslCompiledTransform();
				transformer.Load(sTransformName);

				// add any parameters
				XsltArgumentList args;
				AddParameters(out args, parameterList);

				// setup output file
				writer = File.CreateText(sOutputName);

				// load input file
				XmlTextReader reader = new XmlTextReader(sInputPath);
				reader.ProhibitDtd = false;
				reader.EntityHandling = EntityHandling.ExpandEntities;

				// Apply transform
				transformer.Transform(reader, args, writer);
			}
			finally
			{
				if (writer != null)
					writer.Close();
			}
#else // not UsingDotNetTransforms
			//.Net framework XML transform is still slower than something like MSXML2
			// (this is so especially for transforms using xsl:key).
			MSXML2.XSLTemplate60Class xslt = new MSXML2.XSLTemplate60Class();
			MSXML2.FreeThreadedDOMDocument60Class xslDoc = new
				MSXML2.FreeThreadedDOMDocument60Class();
			MSXML2.DOMDocument60Class xmlDoc = new MSXML2.DOMDocument60Class();
			MSXML2.IXSLProcessor xslProc;

			xslDoc.async = false;
			xslDoc.setProperty("ResolveExternals", true);
			xslDoc.setProperty("ProhibitDTD", false);
			xslDoc.load(sTransformName);
			xslt.stylesheet = xslDoc;
			xmlDoc.setProperty("ResolveExternals", true);
			xmlDoc.setProperty("ProhibitDTD", false);
			xmlDoc.async = false;
			xmlDoc.load(sInputPath);
			xslProc = xslt.createProcessor();
			xslProc.input = xmlDoc;
			AddParameters(parameterList, xslProc);
			xslProc.transform();
			StreamWriter sr = File.CreateText(sOutputName);
			sr.Write(xslProc.output);
			sr.Close();
#endif // UsingDotNetTransforms
#if DEBUG
			DateTime end = DateTime.Now;
			Debug.WriteLine("\tEnding at: " + end.TimeOfDay.ToString());
			System.TimeSpan diff = end.Subtract(start);
			Debug.WriteLine("\tProcess took: " + diff.ToString() + " " + sOutputName);
#endif
		}