Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to transform XML to HTML with XSLT in C#?

Tags:

XSLT newbie question: Please fill in the blank in the C# code fragment below:

public static string TransformXMLToHTML(string inputXml, string xsltString) {   // insert code here to apply the transform specified by xsltString to inputXml    // and return the resultant HTML string.   // You may assume that the xslt output type is HTML. } 

Thanks!

like image 817
Shaul Behr Avatar asked Nov 22 '09 09:11

Shaul Behr


People also ask

How XML is converted into HTML format using XSLT?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

Is XML with XSLT equivalent to HTML?

Before learning XSLT, we should first understand XSL which stands for EXtensible Stylesheet Language. It is similar to XML as CSS is to HTML.

What can be used to transform XML into HTML?

With XSLT you can transform an XML document into HTML.


2 Answers

How about:

public static string TransformXMLToHTML(string inputXml, string xsltString) {     XslCompiledTransform transform = new XslCompiledTransform();     using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) {         transform.Load(reader);     }     StringWriter results = new StringWriter();     using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) {         transform.Transform(reader, null, results);     }     return results.ToString(); } 

Note that ideally you would cache and re-use the XslCompiledTransform - or perhaps use XslTransform instead (it is marked as deprecated, though).

like image 103
Marc Gravell Avatar answered Oct 22 '22 21:10

Marc Gravell


Just for fun, a slightly less elegant version that implements the caching suggested by Marc:

    public static string TransformXMLToHTML(string inputXml, string xsltString)     {         XslCompiledTransform transform = GetAndCacheTransform(xsltString);         StringWriter results = new StringWriter();         using (XmlReader reader = XmlReader.Create(new StringReader(inputXml)))         {             transform.Transform(reader, null, results);         }         return results.ToString();     }      private static Dictionary<String, XslCompiledTransform> cachedTransforms = new Dictionary<string, XslCompiledTransform>();     private static XslCompiledTransform GetAndCacheTransform(String xslt)     {         XslCompiledTransform transform;         if (!cachedTransforms.TryGetValue(xslt, out transform))         {             transform = new XslCompiledTransform();             using (XmlReader reader = XmlReader.Create(new StringReader(xslt)))             {                 transform.Load(reader);             }             cachedTransforms.Add(xslt, transform);         }         return transform;     } 
like image 23
Dathan Avatar answered Oct 22 '22 21:10

Dathan