Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed an XSLT file in a .NET project to be included in the output .exe?

Tags:

c#

project

csproj

I have a simple C# Console App that reads in an XML file specified the the user, runs an XSLT transformation on it, and outputs the results.

When I distribute my app to users, I want to distribute a single .EXE file. My source code consists of 3 files: the .csproj file, the .cs code file, and a .xslt stylesheet.

How can I set up the csproj so the .xslt is "embedded" within the output and cannot be seen or modified by the end user?

Seems easy, but I can't figure it out and Google isn't being too useful.

like image 837
Mike Avatar asked Nov 17 '09 13:11

Mike


People also ask

How do you link an XSLT stylesheet to an XML document?

Link the XSL Style Sheet to the XML Documentxml version="1.0" encoding="UTF-8"?> <? xml-stylesheet type="text/xsl" href="cdcatalog.

How can XSLT transform XML into HTML explain with example?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.


1 Answers

Add the file to your project, then select the file and go to the Properties window (press F4). Set the build action to "Embedded resource". This will cause the file to be embedded into the exe file as a resource.

using(Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourAssemblyName.filename.xslt"))
using (XmlReader reader = XmlReader.Create(strm))
{
    XslTransform transform = new XslTransform();
    transform.Load(reader);
    // use the XslTransform object
}
like image 161
Fredrik Mörk Avatar answered Oct 20 '22 17:10

Fredrik Mörk