XSLT Processing in .NET

In .NET, the System.Xml.Xsl.XslTransform class is used for transforming XML data using an XSLT stylesheet. System.Xml.Xsl.XslTransform supports the XSLT 1.0 syntax, using the http://www.w3.org/1999/XSL/Transform namespace.

Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath
Imports System.IO
Imports System.Text

' Use XPathDocument to transform the Xml String to Html String
'
' piXmlString - Xml String
' piXslPath - xsl file
' piArgumentList - an array of two dimension strings
'                  1st dimension contains name and value
'                  2nd dimension contains number of items

Public Function TransformXmlWithEncoding(ByRef piXmlString As String _
, ByVal piXslPath As String _
, ByVal piEncoding As String _
, Optional ByVal piArgumentList(,) As String = Nothing _
, Optional ByVal piEnableDocumentFunction As Boolean = False _
, Optional ByVal piEnableScript As Boolean = False) As String
        ' Create the XslTransform object.
        Dim oXSL As New XslCompiledTransform
        ' Create the XsltSettings object with script enabled.
        Dim oSettings As New XsltSettings(piEnableDocumentFunction, piEnableScript)

        ' load XSL object with style sheet
        oXSL.Load(piXslPath, oSettings, New XmlUrlResolver())

        Dim oXsltArgumentList As XsltArgumentList = Nothing

        If Not piArgumentList Is Nothing Then
            Dim count As Integer
            Dim total As Integer

            total = piArgumentList.GetUpperBound(1)
            oXsltArgumentList = New XsltArgumentList()

            For count = 0 To total
                If piArgumentList(0, count) <> String.Empty Then
                    oXsltArgumentList.AddParam(piArgumentList(0, count), "", piArgumentList(1, count))
                End If
            Next
        End If

        ' An object implementing the IXPathNavigable interface
        Dim oPathDoc As New XPathDocument(New StringReader(piXmlString))
        Dim sContent As String

        Dim oStream As New System.IO.MemoryStream
        Dim oEncoding As Encoding = Encoding.GetEncoding(piEncoding)
        Dim oXmlTextWriter As New System.Xml.XmlTextWriter(oStream, oEncoding)

        oXSL.Transform(oPathDoc, oXsltArgumentList, oXmlTextWriter)

        oXmlTextWriter.Flush()
        oStream.Position = 0

        Dim oStreamReader As New StreamReader(oStream)

        sContent = oStreamReader.ReadToEnd()

        Return sContent
    End Function