Problem
How do I embed an Xslt file into an assembly so that I won’t have to deploy the file together with the assembly, set configuration options to refer to the file, etc?
Solution
- Create a resource (.resx) file in the project
- In the resource designer, click “Add Resource” and choose “Add Existing File…”. Select the Xslt file.
- Give the new resource a describing name, such as “FilterContentXslt”. The contents of the Xslt file will be available in a string property with this name in the Resource manager.
- Code that performs the transformation:
// Parse the content into an XmlDocument XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlValue); // Retrieve the embedded resource containing the XSLT transform XmlDocument xsltDoc = new XmlDocument(); xsltDoc.LoadXml(Resources.FilterContentXslt); XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(xsltDoc); // Perform the transformation StringWriter writer = new StringWriter(); trans.Transform(doc, writer); string newXmlValue = writer.ToString();
Simple, and it works.
/Emil