This is a continuation post in the Dictionary series. The previous post was on creating, reading and manipulating a Dictionary variable manually - Dictionary Part 1 - Manually Creating a Dictionary
This post will be about querying data from some XML, storing the results into a Dictionary variable and how to iterate through each result.
There are a number of reason why you would have XML. The main one is the result from a Web Service call. There's also the chance that you are getting XML data from another LOB system or an XML files in a SharePoint Document Library. All good reasons for XML data and also for a Workflow to be able to get at that data and pull out what it needs.
We start off with the following XML :
<?xml version="1.0" encoding="utf-16"?>
<Text>The Hunger Games</Text>
The simple XPath expression to get at the Key looks like this : //MetaDataCollection/MetaData/Key
We want the internal Text of the Key node and we are storing the results in a Dictionary variable named dictKeys.
Before we start parsing the data, we need to find out how many items are in our resulting dictionary. We use the Count Items in a Dictionary action.
We store the results in a Number variable. This will tell us how times we need to go through a loop in order to iterate through each value in the Dictionary.
To iterate through each result in the Dictionary there's a few things that needs to be done.
The first, is that we need an Integer variable. This will be an index into our dictionary and when you create the integer variable, the default value it will have is 0 (zero).
The second is the loop itself. We will use the Loop n Times, but you could do a Loop with Condition action also.
Since we have our number variable which tells us how many items are in the dictionary, we can use that for the Loop action.
Within the loop, there are two core actions that are needs. The first is the Get an Item from a Dictionary action. This is the action that lets you pull out data from the dictionary. It's a powerful action, since it can take an Item Name or a Path to pull data out. In our case, since the results from the Query XML will be a single collection of data, we are going to be pulling data out of the dictionary based on an index.
In this case, we insert the index between brackets.
eg. the first index would be (0) and the second would be (1).
Since the data we have in the XML is text, we are storing the result in a Text variable.
Finally, we need an action that will increment the index as we go through the collection of data in the Dictionary.
We use the Do Calculation action with a plus operator and a 1 for the second operand.
As you can see, it's quite straight forward to iterate through a result set in a Dictionary. Things can get a little more hairy with Dictionaries, because of the type of data they can hold and how powerful that can be in workflow design.
If you have any questions or comments, please post them at the bottom of this post.
I've attached an example workflow to this post, should you need to import it into your O365 site to get a better feel for this process.