Using SSIS to convert csv to xml

I have a sample CSV file containg data in the following format:


I need to convert this csv data into xml format which will provide the xml source for an SSIS package I have written. The xml source needs to be in the following format:

   <addressLine1>31 Marlborough Road</addressLine1>
   <postCode>BD18 3NX</postCode>
     <centre id="1">

Is it possible to transform the CSV data to XML in SSIS? Thanks
Who is Participating?
meningaConnect With a Mentor Author Commented:
smar, theres a script component within the data flow task in SSIS 2005 and I have used this for the conversion (an existing stored procedure needs this xml as a parameter).  The script is in VB.Net and I have posted it below in case you find this handy in the future...
I was tempted to write a web method in c# and convert the CSV into xml using xslt but as I will be needing to process large amounts of data, I thought it would be better to use an SSIS based approach. Then we can just place the CSV in the required directory and launch the SSIS package and our DB is updated appropriately.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Imports System.IO
Imports System.Reflection
Public Class ScriptMain
    Inherits UserComponent
    Private targetFile As String
    Private xmlWriter As StreamWriter
    Private rootElement As String = "Data"
    Private rowElement As String = "User"
    Private columns As Integer()
    Private columnames As String()
    Public Overrides Sub AcquireConnections(ByVal Transaction As Object)
        targetFile = CType(Me.Connections.TestConnection.AcquireConnection(Nothing), String)
    End Sub
    Public Overrides Sub PreExecute()
        xmlWriter = New StreamWriter(targetFile, False)
        Dim input As IDTSInput90 = ComponentMetaData.InputCollection(0)
        ReDim columns(input.InputColumnCollection.Count)
        columns = Me.GetColumnIndexes(input.ID)
        Dim column As IDTSInputColumn90
        ReDim columnames(input.InputColumnCollection.Count)
        Dim counter As Integer
        counter = 0
        For Each column In Me.ComponentMetaData.InputCollection(0).InputColumnCollection
            columnames(counter) = column.Name
            counter = counter + 1
    End Sub
    Public Overrides Sub PostExecute()
        xmlWriter.WriteLine(FormatElement(rootElement, True))
    End Sub
    'Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    '    Dim column As IDTSInputColumn90
    '    Dim rowType As Type = Row.GetType()
    '    Dim columnValue As PropertyInfo
    '    With xmlWriter
    '        .Write(FormatElement(rowElement))
    '        For Each column In Me.ComponentMetaData.InputCollection(0).InputColumnCollection
    '            columnValue = rowType.GetProperty(column.Name)
    '            Try
    '                .Write(FormatElement(column.Name) + columnValue.GetValue(Row, Nothing).ToString() + FormatElement(column.Name, True))
    '            Catch ex As Exception
    '                .Write(FormatElement(column.Name) + "" + FormatElement(column.Name, True))
    '            End Try
    '        Next
    '        .WriteLine(FormatElement(rowElement, True))
    '    End With
    'End Sub
    Public Overrides Sub ProcessInput(ByVal InputID As Integer, ByVal Buffer As Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer)
        While Buffer.NextRow()
            Dim counter As Integer
            counter = 0
            For Each index As Integer In columns
                Dim value As Object = Buffer(index)
                xmlWriter.Write(FormatElement(columnames(counter).ToString()) + value.ToString() + FormatElement(columnames(counter).ToString(), True))
                counter = counter + 1
            xmlWriter.WriteLine(FormatElement(rowElement, True))
        End While
    End Sub
    Private Function FormatElement(ByVal elementName As String) As String
        Return FormatElement(elementName, False)
    End Function
    Private Function FormatElement(ByVal elementName As String, ByVal closingTag As Boolean) As String
        Dim returnValue As String
        If closingTag Then
            returnValue = "</"
            returnValue = "<"
        End If
        returnValue += elementName + ">"
        Return returnValue
    End Function
End Class

Open in new window

Yes it is possible. You can import the CSV into a SQL Server table and then use SELECT FOR XML AUTO statement. Try this link:

msdn defintion

Longer text from magazin:
meningaAuthor Commented:
Thanks smar. I want to avoid using a staging table so I've worked out how to transform the data using the Script Component. This XML now complies to the format required to execute a stored procedure.
Congratulations. Do you mean SSIS Script task? In version 2005 it can be coded only in VB.NET - it has nothing to do with C#.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.