May 24, 2006

Reading XSLT From A Manifest Resource annd Applying It

public static string ApplyXslFromManifest(string sourceXml)
  Assembly myAssembly = Assembly.GetAssembly(
typeof(assemblyName)); Stream fileStream = myAssembly.GetManifestResourceStream(
"Common.StripComments.xslt"); StreamReader streamReader = new StreamReader(fileStream); string text = streamReader.ReadToEnd(); return ApplyXslTransform(sourceXml, text); } public static string ApplyXslTransform(
string sourceXml, string xslStyleSheet) { //the outputs string result=""; // read XML XmlTextReader xmlReader = new XmlTextReader(new
StringReader(sourceXml)); // read XSLT XmlTextReader xsltReader = new XmlTextReader(new
StringReader(xslStyleSheet)); XslTransform xslt = new XslTransform (); xslt.Load(xsltReader, null, null); //create the output stream StringBuilder sb = new StringBuilder(); TextWriter outputWriter = new StringWriter(sb); // Transform to a new XPath document XPathDocument xPathDocument = new XPathDocument(
xmlReader); xslt.Transform(xPathDocument, null, outputWriter,
null); //get result result=sb.ToString(); return result; }

No comments: