title: VB.Net apply XSL transformation to XML file
body: <p>I have got some XML which is built by my application. This XML is dropped to an XML file, which I then wish to apply an XSL stylesheet to in order to convert it to a HTML page. However, every time, it just keeps coming out with the original XML rather than the transformed HTML</p>
  
  <p>Here is the XML:</p>
  
  <pre><code>&lt;firelist&gt;
    &lt;visitor&gt;
      &lt;Title&gt;Mr&lt;/Title&gt;
      &lt;Forename&gt;Gregory&lt;/Forename&gt;
      &lt;Surname&gt;House&lt;/Surname&gt;
      &lt;Visiting&gt;asasasas&lt;/Visiting&gt;
      &lt;VisitTime&gt;11:41&lt;/VisitTime&gt;
      &lt;PurposeOfVisit&gt;asasasasa&lt;/PurposeOfVisit&gt;
      &lt;BadgeID&gt;a&lt;/BadgeID&gt;
      &lt;Campus&gt;KWA&lt;/Campus&gt;
      &lt;VisitingFrom&gt;Princeton-Plainsboro Teaching Hospital&lt;/VisitingFrom&gt;
      &lt;ImagePath&gt;\\more\DataCard\VisitorPhotos\V0004.jpg&lt;/ImagePath&gt;
    &lt;/visitor&gt;
  &lt;/firelist&gt;
  </code></pre>
  
  <p>Here is the stylesheet :</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  &gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;html&gt;
              &lt;body&gt;
                  &lt;xsl:for-each select="visitor"&gt;
                      &lt;xsl:value-of select="title"/&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/body&gt;
          &lt;/html&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>and here is the code which does the transform :</p>
  
  <pre><code>Dim document As XmlDocument     ''# Xml document root
  Dim navigator As XPathNavigator ''# navigate document
  Dim transformer As XslTransform ''# transform document
  Dim output As StringWriter
  
  document = New XmlDocument()
  document.Load("firelist.xml")
  
  ''# create navigator
  navigator = document.CreateNavigator
  
  ''# load style sheet
  transformer = New XslTransform()
  transformer.Load("firelist.xslt")
  
  ''# transform XML data
  output = New StringWriter()
  transformer.Transform(navigator, Nothing, output)
  
  ''# display transformation in text box
  Console.WriteLine(output.ToString)
  ''# write transformation result to disk
  Dim stream As FileStream = New FileStream("firelist.html", FileMode.Create)
  
  Dim writer As StreamWriter = New StreamWriter(stream)
  writer.Write(output.ToString)
  
  ''# close streams
  writer.Close()
  output.Close()
  </code></pre>
  
  <p>It is ages since I did anything with XSL and .NET so I'm sure I am probably missing something obvious!</p>
  
  <p><strong>UPDATE:
  Here is the code as it currently stands following modifications made as a result of below suggestions...
  Code-Behind:</strong></p>
  
  <pre><code>Dim document As XmlDocument     ' Xml document root
  Dim navigator As XPathNavigator ' navigate document
  Dim transformer As XslCompiledTransform ' transform document
  Dim output As StringWriter
  
  document = New XmlDocument()
  document.Load("firelist.xml")
  
  ' create navigator
  navigator = document.CreateNavigator
  
  ' load style sheet
  transformer = New XslCompiledTransform()
  transformer.Load("firelist.xslt")
  
  ' transform XML data
  output = New StringWriter()
  transformer.Transform(navigator, Nothing, output)
  
  ' display transformation in text box
  Console.WriteLine(output.ToString)
  ' write transformation result to disk
  Dim stream As FileStream = _
     New FileStream("firelist.html", FileMode.Create)
  
  Dim writer As StreamWriter = New StreamWriter(stream)
  writer.Write(output.ToString)
  
  ' close streams
  writer.Close()
  output.Close()
  </code></pre>
  
  <p><strong>XML:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;firelist&gt;
    &lt;visitor&gt;
      &lt;Title&gt;Dr&lt;/Title&gt;
      &lt;Forename&gt;James&lt;/Forename&gt;
      &lt;Surname&gt;Wilson&lt;/Surname&gt;
      &lt;Visiting&gt;bob&lt;/Visiting&gt;
      &lt;VisitTime&gt;11:30&lt;/VisitTime&gt;
      &lt;PurposeOfVisit&gt;dunno&lt;/PurposeOfVisit&gt;
      &lt;BadgeID&gt;4&lt;/BadgeID&gt;
      &lt;Campus&gt;KWA&lt;/Campus&gt;
      &lt;VisitingFrom&gt;Princeton-Plainsboro Teaching Hospital&lt;/VisitingFrom&gt;
      &lt;ImagePath&gt;\\more\DataCard\VisitorPhotos\V0005.jpg&lt;/ImagePath&gt;
    &lt;/visitor&gt;
  &lt;/firelist&gt;
  </code></pre>
  
  <p><strong>XSLT:</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
  &lt;xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
      &lt;xsl:template match="/firelist"&gt;
          &lt;html&gt;
              &lt;body&gt;
                  &lt;xsl:for-each select="visitor"&gt;
                      &lt;xsl:value-of select="title"/&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/body&gt;
          &lt;/html&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>I am still only getting the original XML being output to my HTML file, rather than the HTML which should result from the XML/XSLT Transformation.</p>