XslReader provides an efficient way to read results of an XSL transformation via an
XslReader has been developed and contributed to the Mvp.Xml project by Sergey Dubinets (Microsoft XML Team).Namespace: Mvp.Xml.Common.Xsl
Assembly: Mvp.Xml (in Mvp.Xml.dll)
Here is an example of using XslReader class. First you need to create an
After that create XslReader instance optionally choosing multithreaded or singlethreaded mode and initial buffer size.
Finally start transformation by calling
method and then you can read transformation output via XslReader object, which implements
Basic XslReader usage sample:
//Prepare XslCompiledTransform XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("catalog.xslt"); //Prepare input XML XmlInput input = new XmlInput("books.xml"); //Create XslReader XslReader xslReader = new XslReader(xslt); //Initiate transformation xslReader.StartTransform(input, null); //Now read XSLT output from the reader XPathDocument results = new XPathDocument(xslReader);
//Prepare XslCompiledTransform XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("../../catalog.xslt"); //Prepare XmlResolver to be used by the document() function XmlResolver resolver = new XmlUrlResolver(); resolver.Credentials = new NetworkCredential("user42", "god"); //Prepare input XML XmlInput input = new XmlInput("../../books.xml", resolver); //Create XslReader, multithreaded mode, initial buffer for 32 nodes XslReader xslReader = new XslReader(xslt, true, 32); //XSLT parameters XsltArgumentList prms = new XsltArgumentList(); prms.AddParam("param2", "", "red"); //Initiate transformation xslReader.StartTransform(input, prms); //Now read XSLT output from the reader XPathDocument results = new XPathDocument(xslReader);
XslReader can work in a singlethreaded (fully buffering) or a multithreaded mode.
In a multithreaded mode XslReader runs an XSL transformation in a separate dedicated thread. XSLT output is being recorded into a buffer and once the buffer is full, transformation thread gets suspended. In a main thread XslReader reads recorded XSLT output from a buffer as a client calls XslReader methods. Whenever the buffer is read, the transformation thread is resumed to produce next portion of an XSLT output.
In effect that means that an XSL transformation happens on demand portion by portion as a client calls XslReader methods. In terms of memory footprint that means that at any time at most buffer size of XSLT output is buffered.
In a singlethreaded mode XslReader runs an XSL transformation to the end and records full XSLT output into a buffer (using effective binary representation though). After that it reads the buffer when a client calls XslReader methods. So in this mode before first call to the XslReader.Read() method returns, XSL transformation is over and XSLT output is buffered internally as a whole.
By default XslReader works in a multithreaded mode. You can choose the mode and the buffer size using
arguments when instantiating XslReader object. On small XSLT outputs XslReader performs better in a singlethreaded mode, but on medium and big outputs multithreaded mode is preferrable. You are adviced to measure performance in both modes to find out which suites better for your particular scenario.
XslReader designed to be reused. Just provide another inoput XML or XSLT stylesheet, start transformation and read the output. If the
method is called when previous transformation isn't over yet, it will be aborted, the buffer cleaned and the XslReader object will be reset to an initial state automatically.
XslReader is not thread safe, keep separate instances of the XslReader for each thread.