Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SSIS, how to loop through the XML inside a specific element using XPATH in a Foreach NodeList Enumerator

I am trying to loop through the XML elements of an XML input file in SSIS. For this I am trying to use a 'Foreach Loop Container' with a Foreach Loop Container. Inside the ForEach Loop Container, I'd like to get the xml code as a String Variable.

I am fairly new to SSIS, and have been browsing the web for quite some time, yet could not get it right..

Input file example; input file ex. (Note that there are some cyrillic symbols in there, which may cause issues. However, when I take out the cyrillic symbols, the same error message shows).

I am trying to loop through the nodes.

This is the package I have created so far;

enter image description here

Below the ForEach loop; I have a variable called 'CurrencyRate' which is a String mapped there. ForEach loop

And the XML Task inside the loop. This is just for testing purposes, it should write the last node element to that output file, with tags and values.

XML Task

However, if I run the code as it is, I get the below error message, even though I do not see any NULL values in my data. And I have no idea where the japanese(?) symbol comes from..; error

Any help would be greatly appreciated! SSIS looks like a powerful tool and I would love to be able to be able to wield it.

like image 896
Karel-Jan Misseghers Avatar asked Aug 25 '15 15:08

Karel-Jan Misseghers


People also ask

How does foreach loop work in SSIS?

The Foreach Loop container repeats the control flow for each member of a specified enumerator. SQL Server Integration Services provides the following enumerator types: Foreach ADO enumerator to enumerate rows in tables. For example, you can get the rows in an ADO recordset.

How do I create a foreach loop container in SSIS?

In the SSIS Toolbox, expand Containers, and then drag a Foreach Loop Container onto the design surface of the Control Flow tab. Right-click the new Foreach Loop Container and select Edit. In the Foreach Loop Editor dialog, on the General page, for Name, enter Foreach File in Folder. Select OK.

What is for each item enumerator in for each loop container Why would you use it?

The For Each Item Enumerator is of the Enumerator option available in Foreach Loop Container. It will use to enumerate through a list of static items declared at the For Each Container level.

What is the difference between for loop and foreach loop in SSIS?

A foreach loop is very similar to a for loop, except there is no evaluation phase. This is because you are looping over a collection of objects, and for each object in the collection, you execute a specific statement.


1 Answers

Instead of the Foreach NodeList Enumerator I am now using the Foreach From Variable Enumerator.

This is the entire package;

new package

Using following variables;

variables

With below script in the first Script Task 'CurrencyRates from XML to Array'; This code will get the XML and put each node as an object in an array. This array is then passed to an object variable.

        public void Main()
    {
        // TODO: Add your code here

        System.Collections.ArrayList arr = new System.Collections.ArrayList();

        XmlDocument doc = new XmlDocument();
        doc.Load((String)Dts.Variables["User::file_path"].Value + Dts.Variables["User::file_name"].Value);
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//CurrencyRate"); // You can also use XPath here
        foreach (XmlNode CurrencyRate in nodes)
        {
            arr.Add((String)CurrencyRate.InnerXml);
        }


        Dts.Variables["User::CurrencyRates"].Value = arr;

        Dts.TaskResult = (int)ScriptResults.Success;
    }

This object variable is then looped using the Foreach From Variable Enumerator, mapping the nodes to an object variable 'CurrencyRate_Item'. In the loop, this item is moved in a String variable, which we can then use for our needs.

like image 147
Karel-Jan Misseghers Avatar answered Sep 17 '22 14:09

Karel-Jan Misseghers