SSIS - 将输入转换为XML的VB脚本组件

时间:2011-09-28 09:24:20

标签: sql-server xml vb.net ssis

我是(VB)脚本的新手,我正在尝试在SSIS脚本组件中找到一种方法,将3个输入列转换为一个XML结构化输出列。

Input:

ID  NAME    DATE
1   AAA     2011-01-01
2   BBB     2011-02-01
3   CCC     2011-03-01


Expected Output:

<output>
<row>
    <id>1</id>
    <name>AAA</name>
    <date>2011-01-01</date>
</row>
<row>
<id>2</id>
    <name>BBB</name>
    <date>2011-02-01</date>
</row>
<row>
    <id>3</id>
    <name>CCC</name>
    <date>2011-03-01</date>
</row>
</output>

我正在寻找的解决方案需要可扩展,因为可以有x个列,并且脚本只需要遍历每个列并生成名称标记和值。

我可以通过将每个值输出到一个通用的“param”节点来实现,但我不确定如何获取列名。

    Public Overrides Sub ProcessInput(ByVal InputID As Integer, ByVal Buffer As Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer)

        inputBuffer = Buffer

        MyBase.ProcessInput(InputID, Buffer)
    End Sub

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        Dim xml_string As String = ""
        Dim counter As Integer

        xml_string = "<output>"

        For counter = 0 To inputBuffer.ColumnCount - 1

            xml_string = xml_string + "<param>" + inputBuffer.Item(counter).ToString() + "</param>"

        Next

        xml_string = xml_string + "<output>"

        Row.xmloutput = xml_string

    End Sub

1 个答案:

答案 0 :(得分:1)

SQL代码:

  SELECT [ID]
  ,rtrim([Name]) [Name]
  ,[Date]
  FROM [tempdb].[dbo].[Table1]
  FOR XML PATH('row'), ROOT ('output')

我得到了这个OutPut

  <output>
    <row>
      <ID>1</ID>
      <Name>AAA</Name>
      <Date>2011-01-01</Date>
    </row>
    <row>
      <ID>2</ID>
      <Name>BBB</Name>
      <Date>2011-02-01</Date>
    </row>
    <row>
      <ID>3</ID>
      <Name>CCC</Name>
      <Date>2011-03-01</Date>
    </row>
  </output>