The SSIS Foreach NodeList Enumerator is useful to enumerate the XML nodes. For example, you can traverse to any node in an XML file using this enumerator. In this article, we will show you how to use this SSIS Foreach NodeList Enumerator with an example.
Before we start demonstrating the Foreach NodeList Enumerator in SSIS, Let me show you the data that we are going to use for this example. It is Employee Table present in SQL Server that we are going to use for this example:
Below query will convert the data in Employee table to an XML file
and the data inside the XML file is:
SSIS Foreach NodeList Enumerator Example
In this SSIS Foreach NodeList Enumerator example, we will enumerate the nodes in the above XML file, and copy the Occupation column information into another database table. To do so, First Drag and drop the Foreach Loop Container into the Control Flow region
Double click on it will open the Foreach Loop Editor to configure it. Within the SSIS General tab, Please specify valid Name and Description. Here, we assigned the name as SSIS Foreach NodeList Enumerator
Next, go to the Collections tab to select the Enumerator. Here, we are choosing Foreach NodeList Enumerator because we want to loop over the XML nodes.
The following is the list of properties that you have to configure for this SSIS Foreach NodeList Enumerator.
Document Source Type: This SSIS Foreach NodeList Enumerator property has three options:
- File Connection: It means, the source is in an XML file present in the file system. You can use the next property to select the file.
- Variables: Source is present in the variable. If you select this option, then you have to select the variable name in the next option.
- Direct Input: We want to insert the XML data directly into this editor. For now, we are selecting this option.
As you can see, we inserted the above-specified XML data
Enumerator Type: This Foreach NodeList Enumerator property has four options:
- Navigator: This will enumerate using XpathNavigator
- Node: Enumerate nodes.
- NodeText: This will enumerate text nodes. It is useful to narrow down your search.
- ElementCollection: IT enumerate element nodes returned by the Xpath.
Next, we are providing the path. Below path will navigate to Occupation node present in each Employ under the Employee details node.
Now select the existing variable and assign it to index 0. Once completed, Click OK to finish configuring the Foreach Loop Container.
Next, Drag and drop the Execute SQL Task to Foreach loop container. This task helps us to insert the values into our Empty table.
Next, we are writing the following SQL Query to insert the table name into the SMO_Enumerator table.
INSERT INTO [NodeListEnumerator] ([Occupation]) VALUES (?)
Here question mark will be replaced by the parameter.
Please cross-check all the properties before you start parameter mapping
Next, go to parameter mapping and select the parameter that we assigned in the Foreach Loop container. It means the Foreach loop container will loop through the XML nodes, and store the occupation information in the variable. Next, we are passing that variable into Execute SQL task
Click OK to finish configuring the SSIS Foreach NodeList Enumerator package. Let us Run the package
Let us open the SQL Server management studio and check whether we inserted the Occupation details into the destination table using SSIS Foreach NodeList Enumerator or not