XML Translator Tool
This is the first in a series of small, but hopefully useful, tools I am planning on releasing. My XML Translator is a simple class that accepts XML in various formats and outputs XML in various formats.
The XML Translator will transform:
- File to XML Document Object
- File to String
- File to File
- XML Document Object to XML Document Object
- XML Document Object to String
- XML Document Object to File
Set this up as a class in your App_Code folder then use it in the following way:
dim myTransformer as new xmlTransform
Imports System.Xml
Imports System.Xml.Xsl
Imports System.IO
Public Class xmlTransformer
Public Function FileToXMLDocument(ByVal xmlFile As String, ByVal xsltFile As String, Optional ByVal arguments As XsltArgumentList = Nothing) As XmlDocument
Dim sw As New StringWriter
Dim Writer As XmlWriter = New XmlTextWriter(sw)
' Read in the contents of xmlFile into a new XML Document
Dim InputXML As New XPath.XPathDocument(xmlFile)
InputXML.CreateNavigator()
Dim Transform As XslCompiledTransform = New XslCompiledTransform
Transform.Load(xsltFile)
Transform.Transform(InputXML, arguments, Writer)
' Output the transformed XML and return
Dim OutputXML As New XmlDocument
OutputXML.LoadXml(sw.ToString)
Return OutputXML
End Function
Public Function FileToString(ByVal xmlFile As String, ByVal xsltFile As String, Optional ByVal arguments As XsltArgumentList = Nothing) As String
Dim sw As New StringWriter
Dim Writer As XmlWriter = New XmlTextWriter(sw)
' Read in the contents of xmlFile into a new XML Document
Dim InputXML As New XPath.XPathDocument(xmlFile)
InputXML.CreateNavigator()
' Transform the Input XML using the xsltFile and it's arguments
Dim Transform As XslCompiledTransform = New XslCompiledTransform
Transform.Load(xsltFile)
Transform.Transform(InputXML, arguments, Writer)
' Output the transformed XML as a String and return
Return sw.ToString
End Function
Public Function FileToFile(ByVal xmlFile As String, ByVal xsltFile As String, ByVal outputFile As String) As Boolean
Dim Writer As XmlWriter
Writer = Nothing
' Read in the contents of xmlFile into a new XML Document
Dim InputXML As New XmlDocument
InputXML.Load(xmlFile)
' Transform the Input XML using the xsltFile and save to Output File
Dim Transform As XslCompiledTransform = New XslCompiledTransform
Transform.Load(xsltFile)
Transform.Transform(xmlFile, outputFile)
Return True
End Function
Public Function XMLDocumentToXMLDocument(ByVal xmlDoc As XmlDocument, ByVal xsltFile As String, Optional ByVal arguments As XsltArgumentList = Nothing) As XmlDocument
Dim sw As New StringWriter
Dim Writer As XmlWriter = New XmlTextWriter(sw)
' Transform the Input XML using the xsltFile and it's arguments
Dim Transform As XslCompiledTransform = New XslCompiledTransform
Transform.Load(xsltFile)
Transform.Transform(xmlDoc, arguments, Writer)
' Output the transformed XML and return
Dim OutputXML As New XmlDocument
OutputXML.LoadXml(sw.ToString)
Return OutputXML
End Function
Public Function XMLDocumentToString(ByVal xmlDoc As XmlDocument, ByVal xsltFile As String, Optional ByVal arguments As XsltArgumentList = Nothing) As String
Dim sw As New StringWriter
Dim Writer As XmlWriter = New XmlTextWriter(sw)
' Transform the Input XML using the xsltFile and it's arguments
Dim Transform As XslCompiledTransform = New XslCompiledTransform
Transform.Load(xsltFile)
Transform.Transform(xmlDoc, arguments, Writer)
' Output the transformed XML as a String and return
Return sw.ToString
End Function
Public Function XMLDocumentToFile(ByVal xmlDoc As XmlDocument, ByVal xsltFile As String, ByVal outputFile As String, Optional ByVal arguments As XsltArgumentList = Nothing) As Boolean
Dim sw As New StringWriter
Dim Writer As XmlWriter = New XmlTextWriter(sw)
' Transform the Input XML using the xsltFile and it's arguments
Dim Transform As XslCompiledTransform = New XslCompiledTransform
Transform.Load(xsltFile)
Transform.Transform(xmlDoc, arguments, Writer)
' Output the transformed XML and return
Dim OutputXML As New XmlDocument
OutputXML.LoadXml(sw.ToString)
' Save the Output XML to the Output File
OutputXML.Save(outputFile)
Return True
End Function
End Class








Leave a Reply