How to: Migrate Your XslTransform Code
The new XSLT classes have been designed to be very similar to the existing classes. The XslCompiledTransform class replaces the XslTransform class. Style sheets are compiled using the Load method. Transforms are executed using the Transform method. The following procedures show common XSLT tasks, and compare the code using the XslTransform class versus the XslCompiledTransform class.
To transform a file and output to a URI
-
Code using the XslTransform class.
-
Code using the XslCompiledTransform class.
To compile a style sheet and use a resolver with default credentials
-
Code using the XslTransform class.
-
Code using the XslCompiledTransform class.
To use an XSLT parameter
-
Code using the XslTransform class.
XslTransform xslt = new XslTransform(); xslt.Load("order.xsl"); //Create the XsltArgumentList. XsltArgumentList argList = new XsltArgumentList(); //Create a parameter which represents the current date and time. DateTime d = DateTime.Now; argList.AddParam("date", "", d.ToString()); //Create the XmlTextWriter. XmlTextWriter writer = new XmlTextWriter("output.xml", null); //Transform the file. xslt.Transform(new XPathDocument(filename), argList, writer, null);
-
Code using the XslCompiledTransform class.
XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("order.xsl"); // Create the XsltArgumentList. XsltArgumentList argList = new XsltArgumentList(); // Create a parameter which represents the current date and time. DateTime d = DateTime.Now; argList.AddParam("date", "", d.ToString()); // Create the XmlWriter. XmlWriter writer = XmlWriter.Create("output.xml", null); // Transform the file. xslt.Transform(new XPathDocument(filename), argList, writer);
To enable XSLT scripting
-
Code using the XslTransform class.
-
Code using the XslCompiledTransform class.
To load the results into a DOM object
-
Code using the XslTransform class.
-
Code using the XslCompiledTransform class.
Note: The XslCompiledTransform class does not have a method that returns the XSLT transformation results as an XmlReader object. However, you can output to an XML file and load the XML file into another object.
To stream the results into another data store
-
Code using the XslTransform class.
-
Code using the XslCompiledTransform class.
// Execute the transformation. XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("output.xsl"); MemoryStream ms = new MemoryStream(); xslt.Transform(new XPathDocument("books.xml"), null, ms); // Load the results into an XPathDocument object. ms.Seek(0, SeekOrigin.Begin); XPathDocument doc = new XPathDocument(ms);
See Also
Build Date: