All of this was a great benefit to us in developing the transform engine, but, we ran into one sticky issue (well, one we care about for the purposes of this post, anyhow). The problem was that an xsl transform provides some ways within the xslt language to control how the results are written out. This meant that there were two ways to specify these output options, one in the xslt and one in the WriterSettings passed into an XmlWriter. For example, if you write the following stylesheet:<?xml version="1.0" encoding="UTF-8" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="no"/> <xsl:template match="/"> <hello/> </xsl:template></xsl:stylesheet>You would expect to have your output formatted without indenting because you have specified indent="no". Of course, as you can imagine, indented formatting of xml also happens to be useful for cases where you are using an XmlWriter without any xslt involved. For this reason, you will find that the XmlWriterSettings class also has an Indent property. So, what would you expect to be the result from using the above xslt with the following code?
XmlWriterSettings settings = new XmlWriterSettings();settings.Indent = true;XmlWriter writer = XmlWriter.Create("out.xml", settings);transform.Transform("test.xml", null, writer);You will get the indenting specified in the WriterSettings (indent=true) rather then what is specified in the xslt (indent="no"). Of course, it is neither practical nor desirable to have the user specify the correct settings for each of their stylesheets, so instead the XslCompiledTransform exposes the OutputSettings property. When you call the Load method of the transform, it determines what the correct XmlWriterSettings should look like. In the case that you call one of the Transform() overloads which does not take an XmlWriter, this settings instance will be used to construct a writer on your behalf, but if you are passing in your own writer, then you can either use this instance directly (as you can see above from Oleg's code) or you can Clone() this instance and twiddle whatever particular settings you care about before constructing a writer.