Using XSLT to Transform XML Using C# ASP.NET


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



Copy this code and paste it in your HTML
  1. using System.Xml;
  2. using System.Xml.Xsl;
  3. using System.Xml.XPath;
  4. using System.IO;
  5.  
  6. private string ApplyXSLTransformation()
  7. {
  8. string strHtml;
  9.  
  10. string strXstFile = Server.MapPath("my-xsl.xslt");
  11. XslCompiledTransform x = new XslCompiledTransform();
  12.  
  13. // Load the XML
  14. XPathDocument doc = new XPathDocument(Server.MapPath("my-xml.xml"));
  15.  
  16. // Load the style sheet.
  17. XslCompiledTransform xslt = new XslCompiledTransform();
  18. xslt.Load(strXstFile);
  19. MemoryStream ms = new MemoryStream();
  20. XmlTextWriter writer = new XmlTextWriter(ms, Encoding.ASCII);
  21. StreamReader rd = new StreamReader(ms);
  22. xslt.Transform(doc, writer);
  23. ms.Position = 0;
  24. strHtml = rd.ReadToEnd();
  25. rd.Close();
  26. ms.Close();
  27. return strHtml;
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.