在SQL Server Integration Services(SSIS)中将平面源文件转换为XML

时间:2015-03-23 12:39:00

标签: xml text ssis

我到处寻找。 MSDN文章,论坛和Stack Exchange。但我无法找到解决问题的方法。希望在这里得到帮助。

所以我需要使用SSIS包将文本源文件转换为XML 。 Interwebs上没有经过验证的方法。如果source是SQL Server数据库,则有很多方法可以生成XML文件。但我的要求是直接从Source转换它,中间没有SQL参与。我到达的最接近的是以下从某处获得的代码。

'Here is one way that i can suggest to everyone for converting 
' Flat File to XML using the Script Component "Destination"
' in the data flow task.
'Source: http://www.sqlservercentral.com/Forums/Topic709064-148-1.aspx

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

Public Class ScriptMain
Inherits UserComponent

Private targetFile As String
Private xmlWriter As IO.StreamWriter
Private rootElement As String = "Root"
Private rowElement As String = "Row"
Private columns As Integer()
Private Columnnames As String()

Public Overrides Sub AcquireConnections(ByVal Transaction As Object)
    targetFile = CType(Me.Connections.DesFile.AcquireConnection(Nothing), String)
End Sub

Public Overrides Sub PreExecute()
    xmlWriter = New IO.StreamWriter(targetFile, False)
    xmlWriter.WriteLine(FormatElement(rootElement))
    Dim input As IDTSInput90 = ComponentMetaData.InputCollection(0)
    ReDim columns(input.InputColumnCollection.Count)
    columns = Me.GetColumnIndexes(input.ID)
    Dim column As IDTSInputColumn90
    ReDim Columnnames(input.InputColumnCollection.Count)
    Dim counter As Integer
    counter = 0
    For Each column In Me.ComponentMetaData.InputCollection(0).InputColumnCollection
        Columnnames(counter) = column.Name
        counter = counter + 1
    Next
End Sub

Public Overrides Sub PostExecute()
    xmlWriter.WriteLine(FormatElement(rootElement, True))
    xmlWriter.Close()
End Sub


'Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
'Dim column As IDTSInputColumn90
' Dim rowType As Type = Row.GetType()
'Dim columnValue As Reflection.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(CInt(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 values As New System.Text.StringBuilder
    xmlWriter.Write(FormatElement(rowElement))
    Dim Counter As Integer
    Counter = 0
    For Each Index As Integer In columns
        Dim Value As Object = Buffer(Index)
        xmlWriter.Write(FormatElement(Columnnames(Counter).ToString()) + Value.ToString() + FormatElement(Columnnames(Counter).ToString(), True))
        Counter = Counter + 1
    Next
    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 = "</"
    Else
        returnValue = "<"
    End If
    returnValue += elementName + ">"

    Return returnValue
End Function

End Class

但是我在以下行中收到错误:     targetFile = CType(Me.Connections.DesFile.AcquireConnection(Nothing),String)

我试图用我的连接管理器替换DesFile:conTargetXML。 我得到的错误如下:

  

错误2'conTargetXML'不是'SC_f8187669a2e147ab9d856ec04791a173.vbproj.Connections'的成员

所以我有以下问题:

  1. 我可以使上述代码有效吗?
  2. 是否有任何其他首选方法可以使用任何扩展/附加组件将文本源文件转换为XML,因为SSIS不提供本机方式作为Target XML组件。

请帮忙。

0 个答案:

没有答案