XSL Transformations
It seems to me that the most practical use for a transformation is to transform an XML string that has been assembled from various data calls. This XML string would be the data for a web page or other user interface. There doesn't seem to be a lot of good examples out there of this. SO I put one together and would like some feedback from anyone that would like to improve upon it. Here is the code:
Public Class XForm
Public Function ConvertXmlToHtml( _
ByVal XmlString As String, _
ByVal XslFileName As String _
) As String
Dim utf8encoder As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
Dim inputInBytes() As Byte = utf8encoder.GetBytes(XmlString)
Dim ms As New System.IO.MemoryStream(inputInBytes)
Dim sr As System.IO.StreamReader
Dim xslt As New System.Xml.Xsl.XslTransform
Dim xpn As System.Xml.XPath.XPathNavigator
Dim xpd As System.Xml.XPath.XPathDocument
Dim rs As String
xpd = New System.Xml.XPath.XPathDocument(ms)
ms.Close()
ms = New System.IO.MemoryStream
xslt.Load(XslFileName)
xslt.Transform(xpd, Nothing, ms, Nothing)
xslt = Nothing
xpn = Nothing
xpd = Nothing
sr = New System.IO.StreamReader(ms)
sr.BaseStream.Seek(0, System.IO.SeekOrigin.Begin)
rs = sr.ReadToEnd()
ms.Close()
sr.Close()
ms = Nothing
sr = Nothing
Return rs
End Function
End Class
|