Travis's lame ol' blog

yay, Travis finally changed his blog!

- Monday, July 26, 2010

Embedding an XSLT as a string in a DLL binary

Project Properties - Resources

If you want to distribute a class library that relies on an XSL document and only want to worry about a single DLL then this is probably the easiest solution. In Visual Studio follow these commands (and save): Project Properties → Resources → Add Existing file

In this case, I am embedding a file named Core.xsl with a key named Core. That will generate a bit of code that looks like this:

~/Properties/Resources.resx:

<data name="Core" type="System.Resources.ResXFileRef, System.Windows.Forms">
	<value>..\Resources\Core.xsl;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>

~/Properties/Resources.Designer.cs:

public static string Core {
	get {
		return ResourceManager.GetString("Core", resourceCulture);
	}
}
XSL file properties

Be sure to set the properties of the file so that its Build Action is set as an Embedded Resource.

Now when you compile, the entire contents of the XSL file get embedded directly into the DLL as a string. You can now use the new property like this:

public static XslCompiledTransform GetXsl(XmlDocument xDoc)
{
	bool enableDocumentFunction = false;
	bool enableScript = true;
	XsltSettings xslSettings = new XsltSettings(enableDocumentFunction, enableScript);

	// only enable debugging in DEBUG mode
	bool isDebug = true;
#if (!DEBUG)
	isDebug = false;
#endif

	XslCompiledTransform xsl = new XslCompiledTransform(isDebug);
	xsl.Load(xDoc, xslSettings, new XmlUrlResolver());

	return xsl;
}

public static XslCompiledTransform GetXsl()
{
	XmlDocument xDoc = new XmlDocument();
	xDoc.LoadXml(Properties.Resources.Core);
	XslCompiledTransform xsl = GetXsl(null, xDoc);
	return xsl;
}

Previous XSLT stuff:

Compiling An XSLT Into A DLL Binary

- Monday, July 19, 2010

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();
xsl.Load(typeof(SomeXsltClassName));

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:

Archives Feed your aggregator (RSS 2.0)

<April 2014>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Search

On this page....

Categories

Blogs

Sign In

newtelligence dasBlog 2.2.8279.16125