I'm porting nxslt utility to .NET 2.0 with XslCompiledTransform as XSLT processor and I just found out XslCompiledTransform API is really severe broken. I was writing before that the only Transform() method overload that accepts XmlResolver outputs to XmlWriter. So if you want to create HTML and to have some control over document() function resolving (or just provide user credentials), you are out of luck with XslCompiledTransform. Quite common scenario, isn't it? Too bad, XML Team should hire better testers.
As Anton Lapounov pointed out XmlWriter is .NET 2.0 is actually capable to write not only XML, but also HTML or text. This behavior is controlled by XmlWriterSettings.OutputMethod property. But this property appears to be read-only (setter is internal). And after all this doesn't really solve my problem with nxslt utility porting, because in a general purpose XSLT utility XSLT output method should be controlled by a XSLT stylesheet, not by code that run transformation.
Update: I was wrong here. It can be done just fine with no any hacks, I just didn't get the way it's meant to be done. See more at the next post.
The following works, I hope it will not be fixed as bug.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
XmlWriterSettings settings = new XmlWriterSettings();
XslCompiledTransform tr = new XslCompiledTransform();
tr.Load(new XmlTextReader(new StringReader(@"
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:output method='html' />
</xsl:stylesheet>
")));
XmlWriterSettings writerSettings = tr.OutputSettings;
using (XmlWriter writer = XmlWriter.Create(@"c:\1.html", writerSettings)) {
writer.WriteElementString("input", "");
}
}
}
}
Yuriy