SSIS-在行集中读取标签并存储在表中

时间:2018-10-11 15:37:20

标签: xml ssis

我有一个独特的问题。我正在使用SSIS加载多个文件,并且在ROWSET中我具有文件名和RowCount。如何使用以下命令将其存储在我的ETL日志表中 程序包ID-SRC_FILE_NM-读取的行-

我正在读取同一包中的多个文件。

<ROWSET xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
    <SRC_FILE_NM>tauth_type_fullext_2018-10-08_11-32-07.312.xml</SRC_FILE_NM>
    <RECORDCOUNT>2</RECORDCOUNT>
    <ROW>  <AUTH_TYPE_CDE>LCL</AUTH_TYPE_CDE>  <AUTH_TYPE_DESC>Local</AUTH_TYPE_DESC> </ROW>
    <ROW>  <AUTH_TYPE_CDE>GLB</AUTH_TYPE_CDE>  <AUTH_TYPE_DESC>Global</AUTH_TYPE_DESC> </ROW>
</ROWSET>

1 个答案:

答案 0 :(得分:0)

脚本组件答案:

将这两个名称空间添加到顶部

using System.Xml.Serialization;
using System.Collections.Generic;

在CreateNewOutputRows()上方,放置以下内容来定义XML。我将您的示例粘贴到https://xmltocsharp.azurewebsites.net/中以进行转换。

[XmlRoot(ElementName = "ROW")]
public class ROW
{
    [XmlElement(ElementName = "AUTH_TYPE_CDE")]
    public string AUTH_TYPE_CDE { get; set; }
    [XmlElement(ElementName = "AUTH_TYPE_DESC")]
    public string AUTH_TYPE_DESC { get; set; }
}

[XmlRoot(ElementName = "ROWSET")]
public class ROWSET
{
    [XmlElement(ElementName = "SRC_FILE_NM")]
    public string SRC_FILE_NM { get; set; }
    [XmlElement(ElementName = "RECORDCOUNT")]
    public int RECORDCOUNT { get; set; }
    [XmlElement(ElementName = "ROW")]
    public List<ROW> ROW { get; set; }
    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsi { get; set; }
}

这是您读取输出行中xml的代码:

public override void CreateNewOutputRows()
{
    string filePath = ""; //put your filepath here. The best way is from a variable in SSIS. Also best since you need a foreach loop to get all the files you are processing.
    System.IO.StreamReader sr = new System.IO.StreamReader(filePath);

    ROWSET xml;

    XmlSerializer serializer = new XmlSerializer(typeof(ROWSET));
    xml = (ROWSET)serializer.Deserialize(sr);
    sr.Close(); //So important, otherwise you can't move file to processed

    Output0Buffer.AddRow();
    Output0Buffer.fname = xml.SRC_FILE_NM;
    Output0Buffer.rc = xml.RECORDCOUNT;
    //Note you can get everything at this point but you didn't ask for it.
}