just some sort of lame blog

Compiling an XSLT into a DLL binary

Project Properties - Pre-Build Event

Did you know that Microsoft provides tools to compile XSLT files into DLLs? Neither did I before a client recently suggested that I do that on a recent project. It turns out it's extremely easy.

Fire up your favorite console and run the following:

"%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\bin\xsltc.exe" /out:SomeProjectPath\bin\SomeXsltDllFileName.dll /class:SomeXsltClassName PathAndFileNameOfXslt.xsl

xsltc.exe might be located somewhere else on your PC and be sure to update the appropriate info in the command above. If it works you can add a reference to this new DLL in your project and use the new class in your code:

XslCompiledTransform xsl = new XslCompiledTransform();

You can also add a pre-build event command line to your project:

"%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\bin\xsltc.exe" /out:$(ProjectDir)bin\SomeXsltDllFileName.dll /class:SomeXsltClassName $(ProjectDir)RelativePathAndFileNameOfXsltInProject.xsl

Then every time you hit CTRL+SHIFT+B it will build your XSLT's DLL automagically each time. Here's a sample of what your output should look like:

------ Build started: Project: ProjectName, Configuration: Debug Any CPU ------
Build started 7/22/2010 2:36:11 PM.

Target PreBuildEvent:
    "%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\bin\xsltc.exe" /settings:script+ /out:D:\Projects\ProjectName\bin\SomeXsltDllFileName.dll /class:SomeXsltClassName D:\Projects\ProjectName\Xslt\NameOfXslt.xsl
    Microsoft (R) XSLT Compiler version 3.5.30729
    [Microsoft (R) .NET Framework version 2.0.50727]
    Copyright (C) Microsoft Corporation. All rights reserved.
Target CoreCompile:
    C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:bin\SomeXsltDllFileName.dll /reference:bin\SomeXsltDllFileName.script.dll /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\ProjectName.dll /target:library ClassFiles.cs

Compile complete -- 0 errors, 0 warnings

Target _CopyFilesMarkedCopyLocal:
    Copying file from "bin\SomeXsltDllFileName.dll" to "bin\Debug\SomeXsltDllFileName.dll".
    Copying file from "bin\SomeXsltDllFileName.script.dll" to "bin\Debug\SomeXsltDllFileName.script.dll".
Target CopyFilesToOutputDirectory:
    Copying file from "obj\Debug\ProjectName.dll" to "bin\Debug\ProjectName.dll".
    ProjectName -> D:\Projects\ProjectName\bin\Debug\ProjectName.dll
    Copying file from "obj\Debug\ProjectName.pdb" to "bin\Debug\ProjectName.pdb".

Build succeeded.

Time Elapsed 00:00:02.31

More info:


comments powered by Disqus