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; (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;
Below the ForEach loop; I have a variable called 'CurrencyRate' which is a String mapped there.
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.
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..;
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.
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.
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.
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.
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.
Instead of the Foreach NodeList Enumerator I am now using the Foreach From Variable Enumerator.
This is the entire package;
Using following 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With