【问题标题】:Parsing XML rows using script Component使用脚本组件解析 XML 行
【发布时间】:2016-12-30 01:18:17
【问题描述】:

我有一个平面文件,数据存储在 XML 行中。我正在使用脚本组件作为源解析 XML 行,如下所示。它工作正常,直到在特定行中,我们看不到其中一列。

例如:在源文件的第 12 行,它只有 Col1 和 Col2,没有 Col3。我需要修改下面的 C# 代码,以便当它在一行中找不到列时,它需要返回为 NULL。

public override void CreateNewOutputRows()
{

    string filepath = @"c:\test\test\xmldata.txt";
    string fileContent = new StreamReader(filepath).ReadToEnd();


    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root>"+ fileContent+"</root>");

    XmlNodeList xnl = doc.GetElementsByTagName("TICKET_EXTRACT");


    foreach (XmlNode xn in xnl) {

        Output0Buffer.AddRow();
        Output0Buffer.col1 = xn["col1"].InnerText;        
        Output0Buffer.col2 = xn["col2"].InnerText;
        Output0Buffer.col3 = xn["col3"].InnerText;
    }

【问题讨论】:

    标签: c# ssis-2012


    【解决方案1】:

    您基本上可以做两件事:使用null conditional 运算符:

    Output0Buffer.col3 = xn["col3"]?.InnerText;
    

    (如果xn["col3"]null,则右侧是null

    或将其包装在 if 语句中:

    if (xn["col3"] != null) {
        Output0Buffer.col3 = xn["col3"].InnerText;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 2017-06-10
      • 2021-08-10
      • 2018-05-23
      • 2017-04-16
      • 1970-01-01
      相关资源
      最近更新 更多