?
Solved

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

Posted on 2008-11-05
3
Medium Priority
?
857 Views
Last Modified: 2008-11-05
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

0
Comment
Question by:koleto115
  • 3
3 Comments
 
LVL 8

Expert Comment

by:OBonio
ID: 22884546
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")
0
 
LVL 8

Expert Comment

by:OBonio
ID: 22884554
Actually, you need to use the GetExecutingAssembly method too :

Assembly.GetExecutingAssembly().GetManifestResourceStream("...");
0
 
LVL 8

Accepted Solution

by:
OBonio earned 800 total points
ID: 22884622
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question