XML Variable API
- Last UpdatedJun 10, 2024
- 4 minute read
ASSEMBLY REFERENCES (dlls)
Skelta.Forms.Core
Skelta.Enterprise
System
System.Data
System.Web.Services
System.Xml
Workflow.NET.NET2
Workflow.NET.Web.Designer.NET2
NAMESPACE USED
Workflow.NET
Workflow.NET.Engine
Workflow.NET.Interfaces
Workflow.NET.Web.Designer
Workflow.NET.Engine.Interfaces
Skelta.Repository.List
Skelta.Repository
Skelta.Forms.Core.Controls
Skelta.Forms
Skelta.Enterprise.XmlStorageType
Mapping the value of XML Variable
If the defined XML variable's internal storage is of type Variable, following code has to be included while invoking the workflow.
Workflow.NET.VariablesCollection varCol = new Workflow.NET.VariablesCollection();
string xstring =
"<Employees><Employee><Name>John</Name><Department>Development</Department></Employee></Employees>";
// EmployeeXmlVariable is the name of the XmlVariable defined in the workflow
varCol.Add("EmployeeXmlVariable", "string", xstring);
//Initializes a new instance of the Client class with the specified application and workflow names
Client objSKClient = new Client("MyRepository","MyWorkflow");
//xmlData can be any Content data that is being passed to the workflow
string xmlData = "<Employee><Name>John</Name></Employee>";
//Executes a workflow with a version number, user id, custom XML content and variable collection object; xmlData is the content data
objSKClient.Execute(1,"activedirectory::Bob",xmlData,varCol);
// If the defined Xml variable's internal storage is of type: Content , XmlVariable will take the data from the passed content provided the schema of the Content variable be the same as passed from here.
Updating XML variables
The XML variables defined for a workflow can be updated by getting the current context of the work item and referring to the XML variables, _WorkFlowContext.XmlVariables.Values will give the XML variables defined in the workflow.
Workflow.NET.VariablesCollection varCol = new Workflow.NET.VariablesCollection();
string xstring =
"<Employees><Employee><Name>John</Name><Department>Development</Department></Employee></Employees>";
// EmployeeXmlVariable is the name of the XmlVariable defined in the workflow
varCol.Add("EmployeeXmlVariable", "string", xstring);
//Initializes a new instance of the Client class with the specified application and workflow names
Client objSKClient = new Client("MyRepository","MyWorkflow");
//xmlData can be any Content data that is being passed to the workflow
string xmlData = "<Employee><Name>John</Name></Employee>";
//Executes a workflow with a version number, user id, custom XML content and variable collection object; xmlData is the content data
objSKClient.Execute(1,"activedirectory::Bob",xmlData,varCol);
// If the defined Xml variable's internal storage is of type: Content , XmlVariable will take the data from the passed content provided the schema of the Content variable be the same as passed from here.
Updating the value of the node
The value of a node in the XML can be updated. Here, we will update the "Name" node value of the Employee XML using the following code.
//_WorkFlowContext is the runtime context object
Workflow.NET.Engine.Context _WorkFlowContext;
int executionId = 123;
//Gets the workflow Context for the specified execution id where executionId is the Execution Id (type int) of the workflow.
_WorkFlowContext = Workflow.NET.Engine.Context.GetContext(executionId, "MyRepository");
_WorkFlowContext.XmlVariables["EmployeeXmlVariable"].RawXml = "<Employees><Employee><Name>Kevin</Name><Department>Testing</Department></Employee></Employees>";
//Updating the value of the node
object[] Submittedvalues = new object[1];
//object[1] refers to the node Name
Submittedvalues[0] = "Ken";
_WorkFlowContext.XmlVariables["EmployeeXmlVariable"].SetNodeValue("//Employee/Name", Submittedvalues);
//EmployeeXmlVariable is the name of the XML variable defined in the workflow
//_WorkFlowContext is the runtime context object
XML String(value of the XML variable): <Employees><Employee><Name>Kevin</Name><Department>Testing</Department></Employee</Employees>
Output(Name of the Employee in the variable has been updated):
<Employees><Employee><Name>Ken</Name><Department>Testing</Department></Employee></Employees>
Updating the Root node
To update the root node, the XML nodelist has to be build and the following code has to be executed.
XML String (value of the XML variable): <Employees><Employee><Name>Kevin</Name><Department>Testing</Department></Employee</Employees>
Output (Root node of the XML variable has been changed as shown below):
<Employees><Employee><Name>John</Name><Department>Development</Department></Employee></Employees>
Getting the value of a node of a XML Variable
To get the value of a node, the following code has to be executed.
//_WorkFlowContext is the runtime context object
Workflow.NET.Engine.Context _WorkFlowContext;
int executionId = 123;
//Gets the workflow Context for the specified execution id where executionId is the Execution Id (type int) of the workflow.
_WorkFlowContext = Workflow.NET.Engine.Context.GetContext(executionId, "MyRepository");
//XML string, using which the root node of the XMLvariable has to be updated
string xstring =
"<Employees><Employee><Name>John</Name><Department>Development</Department></Employee></Employees>";
//Creating the XML nodelist
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xstring);
XmlNodeList xnodelist = xdoc.SelectNodes("//Employees/Employee");
//Defining the object array
object[] submittedvalues = new object[xnodelist.Count];
int i = 0;
//Fill the object array
foreach (XmlNode xnode in xnodelist)
{
submittedvalues[i] = xnode;
i = i + 1;
}
//Sets the updated value to the root node. Updates the EmployeeXmlVariable's (the name of the XMLvariable which is defined in the workflow)root node with the data specified above ( xstring )
_WorkFlowContext.XmlVariables["EmployeeXmlVariable"].SetNodeValue("//Employees/Employee", submittedvalues);
//_WorkFlowContext is the runtime context object
//_WorkFlowContext is the runtime context object
Workflow.NET.Engine.Context _WorkFlowContext;
int executionId = 123;
//Gets the workflow Context for the specified execution id where executionId is the Execution Id (type int) of the workflow.
_WorkFlowContext = Workflow.NET.Engine.Context.GetContext(executionId, "MyRepository");
//Getting the value of the Name node from the Employee Xml
Object[] values = _WorkFlowContext.XmlVariables["EmployeeXmlVariable"].GetNodeValues("//Employees/Employee/Name");
//If there are many Xml Variables we can loop through the Xml variable collection to get the required Xml Variable
//foreach (Workflow.NET.XmlVariable xVariable in //_WorkFlowContext.XmlVariables.Values)
// {
//Getting the node values of a Xml variable
// if(xVaraiable.Name == "EmployeeXmlVariable")
// {
// GetNodeValues returns an object array
// Object[] values = xVariable.GetNodeValues("//Employees/Employee/Name");
// break;
// }
//}
XML String(value of the XML variable): <Employees><Employee><Name>Kevin</Name><Department>Testing</Department></Employee><Employee><Name>John</Name><Department>Testing</Department></Employee></Employees>
Output: values[0]: Kevin
values[1]: John
Getting the XMLNodeList from XML Variable
//_WorkFlowContext is the runtime context object
Workflow.NET.Engine.Context _WorkFlowContext;
int executionId = 123;
//Gets the workflow Context for the specified execution id where executionId is the Execution Id (type int) of the workflow.
_WorkFlowContext = Workflow.NET.Engine.Context.GetContext(executionId, "MyRepository");
//Getting XmlNodeList from Xml Variable
XmlNodeList nodeList= _WorkFlowContext.XmlVariables["EmployeeXmlVariable"].GetXmlNodes("//Employees/Employee");
//if there are more no. of Xml Variables we can loop through, verify and get the Xml NodeList
//foreach (Workflow.NET.XmlVariable xVariable in _WorkFlowContext.XmlVariables.Values)
//{
// Getting the nodes of a Xml variable
// if(xVariable.Name == "EmployeeXmlVariable")
// {
// XmlNodeList nodelist = //xVaraiable.GetXmlNodes(("//Employees/Employee");
// break;
// }
//}
<Employees><Employee><Name>Kevin</Name><Department>Testing</Department></Employee><Employee><Name>John</Name><Department>Testing</Department></Employee></Employees>
Output: Gives us the Employee collection(XML Node List)
XmlNode1:Employee – Kevin
XmlNode2:Employee – John