views:

462

answers:

4

In a visual studio project I have three layers, Data Layer, Business Layer and Presentation Layer.

In the Data Layer I have a few XSLT's that transform some objects into an email, all works fine but I have discovered that the XSLTs do not get built/copied when building.

I have currently, created a folder in the deploy location and placed the XSLT's there but I am concerned about relying on a manual process to update these.

Has anyone encountered a similar issue and if so how did they get around it.

It smacks of changing the MSBuild script to copy the build artifacts to the required location, does anyone have examples of this?

Thaks

A: 

Obvious question maybe, but still has to be asked, did you include the folder containing the XSLT's in the project itself? Is this a web or forms app?

Kev
A: 

In VS, it is easy to set the properties of the XSLT files in the project to copy on build, by default they do not.

kenny
A: 

I may have explained myself poorly.

THe Data layer is a class library that a the presentation layer references.

On building the DataLayer I can get the XSLTs to output to the Bin directory of the DataLayer. However when I build and publish the presentation layer, it correctly grabs the DLL but not the XSLTs

Dean
+2  A: 

If you are using Visual Studio 2005/2008, the easiest way to do this is by including your XSLT files as project resources.

  1. Open the Properties for your project.
  2. Select the Resources tab. You will probably see a link that says "This project does not contain a default resources file. Click here to create one." Go ahead and click on that.
  3. Click the Add Resource drop-down near the top and select Add Existing File.
  4. Browse to your XSLT files and select them.

After you have done this, you can easily access the resources in the following manner:

// To get the contents of the resource as a string:
string xslt = global::MyNamespace.Properties.Resources.MyXsltFile;
// To get a Stream containing the resource:
Stream xsltStream = global::MyNamespace.Properties.Resources.ResourceManager.GetStream("MyXsltFile");


If you are using Visual Studio 2003, your best bet is to include those XSLT files as embedded resources for the DLL. In Visual Studio, select the file(s) in Solution Explorer, open the Properties pane, and change the Build Type to "Embedded Resource". You can then use the GetManifestResourceStream method to get a Stream containing the XSLT(s). The name to pass will be based on the default namespace of your assembly, the folder containing the file, and the name of the file.

For example, say your data layer assembly has a default namespace of My.DataLayer. Within your data layer project you have a folder named Templates which contains a file called Transform.xslt. The code to get your XSLT would look like this:

// There are numerous ways to get a reference to the Assembly ... this way works
// when called from a class that is in your data layer.  Have a look also at the
// static methods available on the Assembly class.
System.Reflection.Assembly assembly = (GetType()).Assembly;
System.IO.Stream xsltStream = assembly.GetManifestResourceStream("My.DataLayer.Templates.Transform.xslt");

For more information check out this article on CodeProject.

jon without an h
Just a note that with 2005, following the exact procedure here to get the resource as a stream I get "System.InvalidOperationException : Resource 'xxx' was not a Stream - call GetObject instead".
tjmoore