Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including an XSLT file into an executable

I'm trying to build an executable which applies XSLT transforms onto a large number XML files. Now my problem is that I'd like to include/refer to the XSLT file stored with my C# VS 2010 solution, so that when I repackage this for another machine, I don't have to copy across the XSLT files. Is this possible?

string xslFile = "C:\template.xslt";
string xmlFile = "C:\\file00324234.xml";
string htmlFile = "C:\\output.htm";

XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xslFile);
transform.Transform(xmlFile, htmlFile);
like image 439
wonea Avatar asked Sep 15 '10 09:09

wonea


1 Answers

You can include the XSLT as an Embedded Resource into your assembly as described here:

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

Once embedded, you can use the transform as follows:

using(Stream stream = Assembly.GetExecutingAssembly()
    .GetManifestResourceStream("YourAssemblyName.filename.xslt"))
{
    using (XmlReader reader = XmlReader.Create(stream))
    {
        XslCompiledTransform transform = new XslCompiledTransform ();
        transform.Load(reader);
        // use the XslTransform object
    }
}
like image 130
Dirk Vollmar Avatar answered Oct 02 '22 00:10

Dirk Vollmar