Minimalist XSLT Transform Using Two Strings


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public string TransformXML(string xml, string xslt)
  2. {
  3. string output = string.Empty;
  4.  
  5. XPathDocument xpd = new XPathDocument(new StringReader(xml));
  6.  
  7. XslCompiledTransform transform = new XslCompiledTransform(true);
  8. transform.Load(new XmlTextReader(xslt, XmlNodeType.Document, null));
  9.  
  10. StringWriter sr = new StringWriter();
  11. transform.Transform(xpd.CreateNavigator(), null, sr);
  12. output = sr.ToString();
  13.  
  14. return output;
  15. }
  16.  
  17.  
  18. //Note that the XSL needs namespace prefixes to make .Net happy:
  19.  
  20. xsl = @"<xsl:stylesheet version='1.0'
  21. xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  22. <xsl:output method='text'/>
  23. <xsl:template match='msg'>Found it!</xsl:template>
  24. </xsl:stylesheet>";
  25.  
  26.  
  27. xml = @"<msg/>";

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.