Outputting HTML with XslCompiledTransform and XmlResolver

| 3 Comments | 1 TrackBack

I was wrong in my last post. Here is how one can output HTML with XslCompiledTransform when XmlResolver needs to be passed to Transform() method.

using (XmlReader src = XmlReader.Create("../../source.xml"))
  XslCompiledTransform xslt = new XslCompiledTransform();
  XmlWriter result = XmlWriter.Create(Console.Out, xslt.OutputSettings);
  xslt.Transform(src, null, result, new XmlUrlResolver());
The key line is emphasized. One just needs to pass XslCompiledTransform's OutputSettings (after XSLT stylesheet is loaded) to XmlWriter.Create() method and then resulting XmlWriter will output transformation results according to <xsl:output> settings in the XSLT stylesheet. Really nice once I get it.

So my the only left complaint about XslCompiledTransform's API is the lack of Transform(IXpathNavigable, XsltArgList, XmlWriter, XmlResolver) method. That means that if by any bad chance you happen to be transforming already loaded XmlDocument or XPathDocument and need to provide XmlResolver - you can't. You will need to pass XmlReader over your XmlDocument or XPathDocument (using XPathNavigator.ReadSubTree() method) and then XslCompiledTransform will load passed XmlReader into another XPathDocument! So your source XML in memory will be duplicated with no reason.

Related Blog Posts

1 TrackBack

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/467

Кто такие MVP from Alexander Lozhechkin [MSFT] on July 14, 2005 9:18 AM

Я довольно часто получаю вопросы о том, кто такие MVP, как стать MVP и т.д. Пост Read More


Hi, just to thank you for this post. I was having this problem for over a year, and my only workaround so far was doing the transformation from disk (by saving the original XML file).

Best regards,

You just made my day with this info. I couldn't output text, but now I can. Thanks.

If anyone is interested, I have just a related post on the what's and why's of XslCompiledTransform.OutputSettings. You can find it at:

Leave a comment