If you want to transform XML file into formatted HTML you can use combination of stylesheet and the XslCompiledTransform class (contained in the System.Xml.Xsl namespace). Note: In the next example will be used stylesheet explained in the article How to use XSL to transform XML content. The next code performs this transformation and saves the result to a new file:

 

Dim XslFile As String = Server.MapPath(“BooksList.xsl”)

Dim XmlFile As String = Server.MapPath(“BooksList.xml”)

Dim HtmlFile As String = Server.MapPath(“BooksList.htm”)

Dim Transform As XslCompiledTransform = New XslCompiledTransform()

Transform.Load(XslFile)

Transform.Transform(XmlFile, HtmlFile)

 

In a dynamic web application you should transform XML file and return the resulting code directly, without generating an HTML file.  In this case you need to create an XPathNavigator for the source XML file. Then you can pass the XPathNavigator to the

XslCompiledTranform.Transform() method and retrieve the results in any stream object. The following code demonstrates this approach:

 

‘ Create an XPathDocument.

Dim XmlFile As String xmlFile = Server.MapPath(“BooksList.xml”)

Dim xDoc As XPathDocument = New XPathDocument(New XmlTextReader(XmlFile))

 

‘ Create an XPathNavigator.

Dim Xnav XPathNavigator  = Xdoc.CreateNavigator()

 

‘ Transform the XML.

Dim Ms As MemoryStream = New MemoryStream()

Dim Args As XsltArgumentList = New XsltArgumentList()

Dim Transform As XslCompiledTransform = New XslCompiledTransform()

Dim XslFile As String = Server.MapPath(“BooksList.xsl”)

Transform.Load(XslFile)

Transform.Transform(Xnav, Args, Ms)

 

Once you have the results in a MemoryStream, you can create a StreamReader to retrieve them as a string:

 

Dim R As StreamReader = New StreamReader(Ms)

Ms.Position = 0

Response.Write(R.ReadToEnd())

R.Close()