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

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);

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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")
Actually, you need to use the GetExecutingAssembly method too :

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();
            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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.