Access to XSLT file as a resource from the same project?

I have an XSLT file that I want to download and use to convert an XML file. I added the file to the same project as the code that uses it, and placed it in the Resources folder and set the Action action to Resource.

This is the code that is trying to access the file:

XslCompiledTransform myXslTransform = new XslCompiledTransform(); myXslTransform.Load(@"[projectName];component/Resources/OrderManagement/OrderOverview.xslt"); 

... where [project_name] is the name of the project. However, this does not work. I played in different ways, but for some reason it seems to me that this is not so. I am sure that this is just a trifle, but none of the articles I found on the Internet (or here) helped me.

Can anyone help?

+4
source share
2 answers

An employee helped me find a solution. We added the resource through the project properties so that I can easily access its contents and use the following code.

 using (var reader = new StringReader(Resources.OrderOverview)) { using (XmlReader xmlReader = XmlReader.Create(reader)) { myXslTransform.Load(xmlReader); myXslTransform.Transform(fileName, arguments, xmlTextWriter); } } 

This is very similar to what outcoldman suggested with a subtle difference in access to a resource in different ways.

+5
source

Change the build action from Resource to Embedded Resource, after which you can do something like

 XslCompiledTransform myXslTransform = new XslCompiledTransform(); var assembly = typeof(SomeTypeFromAssemblyWithResource).Assembly; using (var stream = assembly.GetManifestResourceStream("Resources.OrderManagement.OrderOverview.xslt")) { using (var xmlReader = XmlReader.Create(stream)) { myXslTransform.Load(xmlReader ); } } 

The resource name in the dll can be complex, so maybe you need to first find out the name of the resource with Assembly.GetManifestResourceNames . The compiler creates a name based on the folder and assembly.

+3
source

All Articles