title: Whitespace stripping with XslCompiledTransform
body: <p>I'm trying to migrate a large app from <code>XslTransform</code> to compiled xsl files and <code>XslCompiledTransform</code>.</p>
  
  <p>The app uses the <code>Xsl</code> to create HTML files, and the transformation data (<code>Xml</code>) was passed to the <code>Xsl</code> with a <code>XmlDataDocument</code>, returned from the database.</p>
  
  <p>I've change all that so now I do (at least temporarily):</p>
  
  <p><strong>C#</strong></p>
  
  <pre><code> public string ProcessCompiledXsl(XmlDataDocument xml)
   {
         StringBuilder stringControl = new StringBuilder();
         XslCompiledTransform xslTran = new XslCompiledTransform();
  
         xslTran.Load(
             System.Reflection.Assembly.Load("CompiledXsl").GetType(dllName)
         );
  
         xslTran.Transform(xml, this.Arguments, XmlWriter.Create(stringControl, othersettings), null);
  
         return stringControl.ToString();
   }
  </code></pre>
  
  <p><strong>XSL (just an example)</strong></p>
  
  <pre><code>...
    &lt;xsl:output method="html" indent="yes"/&gt;
    &lt;xsl:template match="/"&gt;
         &lt;xsl:for-each select="//Object/Table"&gt;
                &lt;a href="#"&gt;
                       some text
                &lt;/a&gt;
         &lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>That works, but the xsl is stripping the whitespaces between the tags outputting:</p>
  
  <pre><code>&lt;a href="#"&gt;
     some text
  &lt;/a&gt;&lt;a href="#"&gt;
     some text
  &lt;/a&gt;&lt;a href="#"&gt;
     some text
  &lt;/a&gt;&lt;a...etc
  </code></pre>
  
  <p>I've tried:</p>
  
  <ul>
  <li>Using <code>xml:space="preserve"</code> but I couldn't get it to work</li>
  <li>Overriding the <code>OutputSettings</code>, but I didn't get any good results (maybe I missed something)</li>
  <li>Using an <code>xsl:output method="xml"</code>, and that works, but creates self closing tags and a lot of other problems</li>
  </ul>
  
  <p>So I don't know what to do. Maybe I'm not doing something right.Any help it's really appreciated.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Just for future references, if you want to tackle this problem leaving every XSL intact, one could try <a href="http://goo.gl/25R33" rel="noreferrer">this C# class</a> I wrote, named <code>CustomHtmlWriter</code>.</p>
  
  <p>Basically what I did is extend from <code>XmlTextWriter</code> and modify the methods that write the <code>start</code> and the <code>end</code> of every tag.</p>
  
  <p>In this particular case, you would use it like this:</p>
  
  <pre><code>    StringBuilder sb = new StringBuilder();
      CustomHtmlWriter writer = new CustomHtmlWriter(sb);
  
      xslTran.Transform(nodeReader, this.Arguments, writer);
  
      return sb.ToString();
  </code></pre>
  
  <p>Hope it helps someone.</p>