Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access an XSLT file as resource from same project?

I have an XSLT file that I want to load and use to transform an XML file. I've added the file to the same project as the code that uses it and put it in the "Resources" folder and set the Build Action to "Resource".

This is the code that tries to access the file:

        XslCompiledTransform myXslTransform = new XslCompiledTransform();

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

... where [projectName] is the name of the project. However this doesn't seem to work. I've played around with different paths, but somehow I don't seem to get it right. I'm sure it's just a little thing, but none of the articles I have found on the internet (or here) have helped me.

Can anyone help?

like image 982
Anne Schuessler Avatar asked Apr 08 '13 15:04

Anne Schuessler


People also ask

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

Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document. For example, add the following line to the document prolog: <? xml-stylesheet type='text/xsl' href='filename.

How do I open an XSLT file?

You can open an XSLT file in any text editor since it's a text-only file. Windows Notepad is the text editor built-in to Windows and can be helpful if you need to quickly make a change, but it's probably not the best program for doing heavy editing.

Is XSLT still relevant?

XSLT is very widely used. As far as we can judge from metrics like the number of StackOverflow questions, it is in the top 30 programming languages, which probably makes it the top data-model-specific programming language after SQL. But XSLT isn't widely used client-side, that is, in the browser.


2 Answers

A co-worker has helped me find a solution. We added the resource via the properties of the project, so that I can access its content easily and used 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 the subtle difference that the resource is accessed differently.

like image 160
Anne Schuessler Avatar answered Sep 28 '22 07:09

Anne Schuessler


Change the build action from Resource to Embedded Resource, after this 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 );
    }
}

Resource name in dll can be tricky so maybe you want first to know the resource name with Assembly.GetManifestResourceNames. Compiler generates name based on the folder and assembly.

like image 24
outcoldman Avatar answered Sep 28 '22 06:09

outcoldman