Using SSIS to convert csv to xml

Posted on 2008-06-10
Last Modified: 2013-11-07
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
Question by:meninga
  • 2
  • 2

Expert Comment

ID: 21751707
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:

Author Comment

ID: 21752734
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.

Expert Comment

ID: 21753813
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#.

Accepted Solution

meninga earned 0 total points
ID: 21758000
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


Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now