Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 27th, 2004, 05:15 AM
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Embedded XSLTs in a C# Project

I'm using an XSLT to transform an "in-memory" XML document into an HTML file. The following code performs the transformation without any problems, but it relies on a hard-coded path:

XPathNavigator nav = xmlDocRoot.CreateNavigator();
XslTransform xslt = new XslTransform();
xslt.Load("C:/SomeFolder/AnotherFolder/MyXSLT.xslt");
XmlTextWriter htmlPage = new XmlTextWriter(htmlBaseFilename + ".html", null);
xslt.Transform(nav, null, htmlPage, null);

Replacing the Load method call with the following call works, provided the XSLT is located in the working folder for the app, and it eliminates the need for a hard-coded path:

xslt.Load("MyXSLT.xslt");

What I really would like to do is embed the XSLT as a resource in the project (no worries about the file getting deleted or modified by some over zealous user), but I haven't had any luck doing so. Some of Microsoft's documentation describes adding the XSLT file to the project, setting the Build property to Embedded Resource, and calling methods from the "GetManifestResource..." group on the executing assembly, but I can't seem to get it to work.

Any ideas on how to embed the XSLT in the project?

 
Old November 1st, 2004, 07:46 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Right click on the project in the Solution Explorer window. In the context menu that pops up, hold the mouse cursor over "Add...". Then left click on "Add Existing Item..." in the next menu that pops up.

This brings up the windows "Open File" window. There are no filters available for "XSLT" extensions in the "Files of Type" combo box, so select the "All Files" filter. Navigate to and select your XSLT file and it becomes part of the project.

Left click to highlight the file in the Solution Explorer list. In the properties window, the "Build Action" property is defaulted to "Content". You can change it to "Embedded Resource" from the combobox cell.
 
Old November 2nd, 2004, 10:23 PM
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know how to get it in the project. I can't figure out how to reference it from the code once it's in there.


 
Old November 9th, 2004, 09:40 PM
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Guess I'll have to answer this myself. After puzzling 'til my puzzler was sore, I came up with this (it's not that complicated, but for some reason it took awhile before it came to me).

1) Add the XSLT file "reportgen.xslt" to the project and set the Build Action to "Embedded Resource" (refer to seblake's reply above).

2) Use the following code to execute the transform:

     XPathNavigator xpathNav = xmlDoc.CreateNavigator();
     XslTransform xslt = new XslTransform();
     XmlTextReader xsltReader =
          new XmlTextReader(Assembly.GetExecutingAssembly().GetM anifestResourceStream
          ("Report.HTML.reportgen.xslt"));
     xslt.Load(xsltReader, null, null);
     StreamWriter reportWriter = new StreamWriter(reportFilename + ".html");
     xslt.Transform(xpathNav, null, reportWriter, null);
     reportWriter.Close();

where the namespace for "reportgen.xslt" is "Report.HTML". Of course, things can be shortened up by making the constructor calls for xsltReader and reportWriter directly from within the argument list of xslt.Load and xslt.Transform.

Note: This example uses an "in memory" XML document contained in the variable "xmlDoc", which is of type XmlDocument. An external file can be loaded and used as well.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded or not NEO1976 EJB 0 April 3rd, 2007 09:45 AM
Website project vs Web Application Project... thenoseknows ASP.NET 2.0 Professional 0 January 14th, 2007 09:47 PM
Embedded Files gygaxx Visual Basic 2005 Basics 2 November 28th, 2006 03:25 PM
"embedded" element??? Marcus CSS Cascading Style Sheets 3 June 1st, 2006 06:42 PM
Merging two xslts into one...help!! nrane26 XSLT 1 January 7th, 2005 04:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.