Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA™ Work Tasks

Loop through XML Document

  • Last UpdatedDec 17, 2025
  • 6 minute read

Perform the following steps to loop through the elements of an XML Document:

  • Create an XML variable and load a predefined schema

  • Create an XML variable of type Xml Iterator and loop through the schema

  • Add a script activity to create the XML and get the data

  • Loop through the elements of the XML document

  • Display value of the XML Iterator

Create XML variable and load predefined schema

  1. Create a new workflow, LoopXmlVariableWorkflow.

    The Process Designer window appears.

  2. Right-click the Start activity, and then click Activity Properties.

    The Activity Properties window appears.

  3. Click on the edit icon near the XmlVariables property.

    The XmlVariables window appears.

  4. Click New Variable to add a new variable.

  5. In the Variable Name field, enter the name and description for the variable.

  6. Select XmlDocument from the Type list.

  7. Create an XML file EmployeeData.XML.

    This file is required to build the XML Schema.

  8. Copy the following schema to create the XML file.

    <Employees>

    <Employee>

    <Name>

    </Name>

    <Department>

    </Department>

    </Employee>

    <Employee>

    <Name>

    </Name>

    <Department>

    </Department>

    </Employee>

    </Employees>

  9. Click Browse and load EmployeeData.XML file from the folder where it is saved.

  10. Click Load to load the schema.

    The XML Variable of type XML Document has been created.

    You will see the schema in the Variable properties area as in the XML document. 

  11. Click Save Variable to save the variable.

Create XML variable of type XmlIterator and loop through schema

Use XML Variable of type XmlIterator to define variables for repeating XML elements and attributes. In this scenario, Employee, Name, and Department are repeating elements.

  1. Create an XML Variable of type Xml Iterator.

    1. Click New Variable and enter the name in the Variable Name field.

    2. Select the type as Xml Iterator.

      The Xml Iterator type option is available only if another variable of type Xml Document exists. Two additional properties appear.

    3. Select the parent XML Variable as Employees.

      The parent variable of an Xml Iterator variable can be another Xml Iterator Variable also.

    4. Select Node as Employee.

      The node should represent the variable. Only nodes in the parent variables (that is, Employees) are displayed.

    5. Click Save Variable to save the variable.

      The XML Variable has been created.

    Add script activity

    In a real time application, you can get XML Data by different means such as invoke form, from a Web Service, or through Script activity. In this scenario you will create an XML using a Script Activity.

    1. Drag a Script activity from the Integration Activities list to the Process Designer area.

    2. Right-click on the Script activity in the Design area and select Activity Properties. The Activity Properties appears.

    3. Enter a Name and Description for the activity.

    4. Select the Programming Language as CSharp.NET in the Language property.

      1. Click on the edit icon in the Code property to write the code.

        To set the value for the XML, you can use the following sample code:

        //Sample CSharp code

        using System;

        using Workflow.NET;

        using Workflow.NET.Engine;

        public class WorkflowScript59e4b2aadf064757a8089c95c630794f

        {

        public void Run(int ExecutionId, int ExecutionDetailsId, Workflow.NET.Engine.Context ctx, Workflow.NET.Action action, string inlink)

        {

        string XmlValues = "<Employees><Employee><Name>Tim</Name><Department>Sales";

        XmlValues += "</Department></Employee><Employee><Name>John</Name><Department>Marketing";

        XmlValues += "</Department></Employee></Employees>";

        ctx.XmlVariables["Employee"].RawXml = XmlValues;

        ctx.SaveVariables();

        }

        }

        • In this script action you are building the values for XML Schema as per the Schema defined for the XML Variable Employee in the Start activity, that is,

          string XmlValues = "<Employees><Employee><Name>Tim</Name><Department>Sales";
          XmlValues += "</Department></Employee><Employee><Name>John</Name><Department>Marketing";
          XmlValues += "</Department></Employee></Employees>"; 

        • ctx is the Workflow.NET.Engine.Context object which encapsulates all the information about the executing workflow. The Context object will have all the Variable, Content, XML Variable Details in the runtime.

        • Assign the runtime value to the XmlVariable Employee as:

          ctx.XmlVariables["Employee"].RawXml = XmlValues;

        ctx.SaveVariables(): Persists the assigned value to the XML variable to the database. After calling the ctx.SaveVariables(), the value of the XML Variable will be available in the runtime for the workflow.

        Now the XML Variable Employee will contain the details of two employees, Tim and John (in XML format) which can be used in the workflow.

      2. After writing the code, click Update to save it. 

        using System;

        using Workflow.NET;

        using Workflow.NET.Engine;

        public class WorkflowScript50e4b2aadf064757a8089c95c630794f

        {

        public void Run(int ExecutionId, int ExecutionDetailsId, Workflow.NET.Engine.Context ctx, Workflow.NET.Action action, string inlink)

        {

        string XmlValues = "<Employees><Employee><Name>Tim</Name></Department>Sales";

        XmlValues += "</Department></Employee><Employee><Name>John</Name><Department>Marketing";

        XmlValues += "</Department></Employee></Employees>";

        ctx.XmlVariables["Employee"].RawXml = XmlValues;

        ctx.SaveVariables();

        }

        }

      Loop through elements of XML document

      The For-Each Loop Activity is used loop through the collection within an Xml Iterator Variable. It allows repeated execution of an activity or a group of activities.

      1. Drag the For-Each Loop activity from the Engine Activities list to the Process Designer area.

      2. Right-click on the For-Each Loop activity in the Design area and click Activity Properties from the shortcut menu.

        The Activity Properties appears.

      3. Enter a name and description for the activity

      4. Select EmployeeIterator from the XML Iterator property list.

      5. Click Save to save all the data entered.

      6. Link the Script activity to the For-Each Loop activity.

        Display value of XML iterator

        To display the looped XML value, an Information activity is required.

        1. Select Human Activities.

        2. Drag Information activity to the Design area.

        3. Right-click the Information activity in the Design area and click Activity Properties from the shortcut menu.

          The Activity Properties appears.

        4. Enter a name and description for the activity.

        5. Configure the Actor to whom the information has to be sent:

          1. Click the edit icon near the Assign Actor(s) property.

            The Assign Actor(s) appears.

          2. Assign the Information task to the user who has alerted the workflow.

            For this, you have to specify a small expression.

          3. Select the required provider from the Providers list.

          4. Select Id from the first drop-down list.

          5. Keep the operator as default "=".

          6. Select Variable from the next drop-down list.

          7. Select Variable.SubmittedBy.ID in the last drop-down list and click Add.

          8. Click Update to save the property value.

        6. Configure the Subject property as explained:

          1. Click the edit icon near the Subject property.

            The Subject appears.

          2. Enter the subject in the text area.

            To customize the subject and append the subject by selecting some XML variable and building an expression follow the next set of steps.

          3. Type 'The Employees Name is:'. in the text area.

          4. Select XML Variables from the first drop-down list.

          5. Select the XML Variable you had declared in the Start activity from the second drop-down list that is, Employee.

          6. Select the Name node from the XML Schema in the third drop-down list and click Append.

          7. Similarly, append the Department value.

          8. Click Update.

        7. Click Save to save all the information you have entered.

        8. Link the For-Each Loop activity and the Employee Information activity you have created.

        9. Right-click on the link to select the link action. Select Step as you have to loop through the XML values and get the value in the Information activity.

          Just as you use the for-loop in normal programming, check whether the count value has been reached. If not, then you have to increment the count.This task is achieved through the Next-Loop activity.

          Note: For every For-Each loop activity, you need a Next-Loop activity.

          The Next-Loop activity will check the count and repeat the execution within the loop. Since you have to loop through the XML variables, add a Next-Loop Activity to the workflow.

          1. Drag the Next-Loop activity from the Engine Activities list to the Design area.

          2. Link the Employee Information activity and the Next-Loop activity.

          3. Add a link from the Next-Loop Activity to the For-Each Loop Activity which completes the loop.

          4. Set the link action as Next Step which is needed to advance the loop.

            Publish LoopXmlVariableWorkflow

            1. Click the Publish option from the menu bar.

              A message workflow published successfully appears.

            2. Select and right-click the LoopXMLVariableWorlfow from the workflow list. Click Publish and Test Run from the shortcut menu.

              The Process execution view appears wherein you can see the execution status. You will notice that all the activities have been executed successfully.

              Note: If the Status is "Execution Pending" in the Process Execution, then click the Refresh button to get the latest status.

              View inbox

              1. Select the Inbox option from the Enterprise Console.

              2. In the Inbox, you can find two Employee Information tasks with the Subject "The Employee Name is John and Employee Department is Marketing" and "The Employee Name is Tim and Employee Department is Sales. You have successfully created the LoopXmlVariableWorkflow.

                For more information about XML Variables and their types, see XML Variables topic in the Developing Business Processes section.

                In This Topic
                TitleResults for “How to create a CRG?”Also Available in