Search code examples
xmlxslttexttransformxslcompiledtransform

Vb.net application to convert xml to text


Need help in creating a console application to read xml files from a folder and convert them to text file and save them in a separate folder using a xsl file for transformation.

I have tried multiple options but nothing has worked. Below is what I currently have.

        For Each xmlFile As System.IO.FileInfo In xmlFiles

            'Do some data processing here
            Document = New XmlDocument()

            Document.Load(xmlFile.FullName)
            navigator = Document.CreateNavigator

            Dim reade As XmlReader = XmlReader.Create(xmlFile.FullName)
            transformer = New XslCompiledTransform
            transformer.Load("C:\Sample_XML_Files\Sample_XML_Files\Testing.xslt")
            output = New StringWriter()
            transformer.Transform(reade, Nothing, output)

            Dim stream As FileStream = New FileStream(xmlFile.DirectoryName + "\Out\" + xmlFile.Name + ".text", FileMode.Create)

            Dim writer As StreamWriter = New StreamWriter(stream)
            writer.Write(output.ToString)

            MessageBox.Show(output.ToString)
            writer.Close()
            output.Close()

        Next

Any help in resolving this will be helpfull.


Solution

  • Try using the Transform method that just takes the two string arguments, input file and destination file. That way you eliminate all the reader/stream stuff on your end. Public Sub Transform (inputUri As String, resultsFile As String)

    I assume you've tried the xml transform via the xslt directly via xml notepad or some other interactive xslt processing program that can use microsoft's xslt parser. This would be to elimate the xml/xslt transform itself as the source of the problem.

    Alternatively you could use a different xslt engine to do your work.