How to embed and access xslt file as a project resource file?

koleto115
koleto115 used Ask the Experts™
on
1. As a part of my project I am using XSLT transformation to transform XML file to HTML format.
Can I avoid the loading of the xslt file from the hard disk and distribute the application with the xslt file as  embedded resource?

I suppose, I have to include the file to the project and on the properties dialog to select: Embedded Resource.

How to access then the file stream in the particular snipped code attached?

2. Is there any version of a kind of Xsl transformation class  that does render direct to a string instead to a file stream?
XslCompiledTransform xslt = new XslCompiledTransform();
                        
string path = Path.GetDirectoryName(Application.ExecutablePath);
            Directory.SetCurrentDirectory(path);
            xslt.Load(@".\report.xslt");

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
In the System.Reflection namespace is the Assembly object.

Assembly.GetManifestResourceStream(string name)

Where name is the assembly's namespace + the project directory + the filename in dotted notation.

e.g. Assembly.GetManifestResourceStream("MyProject.MyXMLAssembly.XSLTDir.MyXSLT.xslt")

Commented:
Actually, you need to use the GetExecutingAssembly method too :

Assembly.GetExecutingAssembly().GetManifestResourceStream("...");
Commented:
Part #2 :

There are overloads to write to a stream or XmlWriter on the Transform method.  None of them take a filename path as their input though so you'll have to read into XmlDocument first and use an XsltArgumentList object.  Something along these lines :



            XslTransform xslt = new System.Xml.Xsl.XslTransform();
            XsltArgumentList xal = new System.Xml.Xsl.XsltArgumentList();
            XmlDocument xml = new XmlDocument();
            xml.Load(myXmlPath);
            MemoryStream transformedXmlBuffer = new MemoryStream();
            XmlTextWriter xtw = new XmlTextWriter(transformedXmlBuffer, Encoding.UTF8);
            xslt.Transform(xmlInput, xal, xtw);
            string transformedXml = Encoding.UTF8.GetString(transformedXmlBuffer.GetBuffer());

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial