Inheritance: System.IO.StringWriter
        private string buildXaml(PD_Process process, PD_Subject subject)
        {
            Flowchart flow = new Flowchart();
            Variable flowGlobalTransition = new Variable<String> { Name = "GlobalTransition" };
            Variable flowGlobalVariables = new Variable<DynamicValue> { Name = "GlobalVariables"};
          


            string globVariablesInit = "";
            if (subject.GlobalParameters.Count > 0)
            {
                globVariablesInit = "{";
                foreach (string p in subject.GlobalParameters)
                {
                    var par = _pdesignerDB.PD_Parameters.Find(subject.PD_Process_Id, p);
                    globVariablesInit = globVariablesInit + "\""+ p +"\":"+ par.Config +",";
                }
                globVariablesInit = globVariablesInit.Remove(globVariablesInit.Length - 1, 1);
                globVariablesInit = globVariablesInit + "}";
            }

            Variable flowGlobalVariablesSchema = new Variable<string> { Name = "GlobalVariablesSchema", Default = globVariablesInit };

            Dictionary<int, FlowStep> nodeList = new Dictionary<int, System.Activities.Statements.FlowStep>();

            flow.Variables.Add(flowGlobalTransition);
            flow.Variables.Add(flowGlobalVariables);
            flow.Variables.Add(flowGlobalVariablesSchema);

            

            foreach (var state in subject.States)
            {
                FlowStep f;
                if (state.Type == PD_StateTypes.FunctionState)
                {
                    var s = (PD_FunctionState)state;
                    string timeout = "";
                    try
                    {
                        var timeouttransition = subject.Transitions.First(result => result.Source == s.Id && result.Type == PD_TransitionTypes.TimeoutTransition);

                        timeout = ((PD_TimeoutTransition)timeouttransition).TimeSpan;
                    }
                    catch (Exception e)
                    {

                    }
                    var transitions = subject.Transitions.Where(result => result.Source == s.Id && result.Type == PD_TransitionTypes.RegularTransition);
                    List<string> titems = new List<string>();
                    transitions.ToList().ForEach(i => titems.Add(((PD_RegularTransition)i).Name));
                    f = new FlowStep() { Action = new FunctionStateT() { DisplayName = s.Name, OrderId = s.Id, name = s.Name, GlobalTransition = new OutArgument<string>(flowGlobalTransition), GlobalVariables = new InOutArgument<DynamicValue>(flowGlobalVariables), isEndState = s.EndState, readableParameters = collectionToString(s.ReadableParameters), editableParameters = collectionToString(s.EditableParameters), TimeOut = timeout, transitions = collectionToString(titems) } };
                }
                else if (state.Type == PD_StateTypes.SendState)
                {
                    var s = (PD_SendState)state;
                    string timeout = "";
                    try
                    {
                        var timeouttransition = subject.Transitions.First(result => result.Source == s.Id && result.Type == PD_TransitionTypes.TimeoutTransition);

                        timeout = ((PD_TimeoutTransition)timeouttransition).TimeSpan;
                    }
                    catch (Exception e)
                    {

                    }
                    var message = process.Messages.First(result => result.Id == s.Message);
                    string to = process.Subjects.First(result => result.Id == message.To).Name;
                    f = new FlowStep() { Action = new SendStateT() { DisplayName = s.Name, OrderId = s.Id, name = s.Name, GlobalTransition = new OutArgument<string>(flowGlobalTransition), GlobalVariables = new InOutArgument<DynamicValue>(flowGlobalVariables), isEndState = s.EndState, readableParameters = collectionToString(s.ReadableParameters), editableParameters = collectionToString(s.EditableParameters), messageType = message.PD_MessageType.Name, parameters = collectionToString(message.PD_MessageType.Parameters), toSubject = to, TimeOut = timeout } };

                }
                else //(state.Type == PD_StateTypes.ReceiveState)
                {
                    var s = (PD_ReceiveState)state;
                    string messages = "";
                    List<string> messagelist = new List<string>();
                    var receivetransitions = subject.Transitions.Where(result => result.Source == s.Id && result.Type == PD_TransitionTypes.ReceiveTransition);

                    foreach (var i in receivetransitions)
                    {
                        messagelist.Add(receiveTranstionToString(process, (PD_ReceiveTransition)i));
                    }
                    messages = collectionToString(messagelist);

                    string timeout = "";
                    try
                    {
                        var timeouttransition = subject.Transitions.First(result => result.Source == s.Id && result.Type == PD_TransitionTypes.TimeoutTransition);
                        timeout = ((PD_TimeoutTransition)timeouttransition).TimeSpan;
                    }
                    catch (Exception e)
                    {

                    }

                    f = new FlowStep() { Action = new ReceiveStateT() { DisplayName = s.Name, OrderId = s.Id, name = s.Name, GlobalTransition = new OutArgument<string>(flowGlobalTransition), GlobalVariables = new InOutArgument<DynamicValue>(flowGlobalVariables), isEndState = s.EndState, TimeOut = timeout, messages = messages } };
                }
                flow.Nodes.Add(f);
                nodeList.Add(state.Id, f);
            }


            var initGP = new FlowStep() { Action = new InitializeGlobalParameters() { DisplayName = "init GP", DynamicVal = new InOutArgument<DynamicValue>(flowGlobalVariables), GlobalParameterSchema = new InArgument<string>(flowGlobalVariablesSchema) } };

            initGP.Next = nodeList[subject.States.First(result => result.StartState == true).Id];
            flow.StartNode = initGP;

            // flow.StartNode = nodeList[subject.States.First(result => result.StartState == true).Id];


            foreach (var state in subject.States)
            {
                List<PD_Transition> transitions = new List<PD_Transition>();
                try
                {
                    subject.Transitions.Where(result => result.Source == state.Id).ToList().ForEach(item => transitions.Add(item));
                }
                catch (Exception e) { }

                if (transitions.Count > 0)
                {
                    if (transitions.Count == 1)
                    {
                        var t = transitions[0];
                        nodeList[t.Source].Next = nodeList[t.Target];
                    }
                    else
                    {
                        FlowSwitch<String> newSwitch = new FlowSwitch<String> { Expression = flowGlobalTransition };
                        flow.Nodes.Add(newSwitch);
                        nodeList[state.Id].Next = newSwitch;

                        try
                        {
                            var timeouttransition = transitions.First(result => result.Type == PD_TransitionTypes.TimeoutTransition);

                            newSwitch.Cases.Add("TimeOut!", nodeList[timeouttransition.Target]);
                            transitions.Remove(timeouttransition);
                        }
                        catch (Exception e) { }

                        if (state.Type == PD_StateTypes.SendState)
                        {
                            newSwitch.Default = nodeList[transitions[0].Target];
                        }
                        else if (state.Type == PD_StateTypes.ReceiveState)
                        {
                            foreach (var t in transitions)
                            {
                                newSwitch.Cases.Add(receiveTranstionToString(process, (PD_ReceiveTransition)t), nodeList[t.Target]);
                            }
                        }
                        else
                        {
                            foreach (var t in transitions)
                            {
                                newSwitch.Cases.Add(((PD_RegularTransition)t).Name, nodeList[t.Target]);
                            }
                        }
                    }
                }
            }

            ActivityBuilder builder = new ActivityBuilder();
            builder.Name = "strICT.InFlowTest.WFProcesses." + process.Name + "." + subject.Name;


            builder.Implementation = flow;

            VisualBasic.SetSettings(builder, new VisualBasicSettings());

            //StringBuilder sb = new StringBuilder();
            StringWriterUtf8 stream = new StringWriterUtf8();
            XamlWriter writer = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(stream, new XamlSchemaContext()));
            XamlServices.Save(writer, builder);


            string res = stream.GetStringBuilder().ToString();
            res = res.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
            return res;
        }
        private string buildXaml(PD_Process process, PD_Subject subject)
        {
            Flowchart flow = new Flowchart();
            Variable  flowGlobalTransition = new Variable <String> {
                Name = "GlobalTransition"
            };
            Variable flowGlobalVariables = new Variable <DynamicValue> {
                Name = "GlobalVariables"
            };



            string globVariablesInit = "";

            if (subject.GlobalParameters.Count > 0)
            {
                globVariablesInit = "{";
                foreach (string p in subject.GlobalParameters)
                {
                    var par = _pdesignerDB.PD_Parameters.Find(subject.PD_Process_Id, p);
                    globVariablesInit = globVariablesInit + "\"" + p + "\":" + par.Config + ",";
                }
                globVariablesInit = globVariablesInit.Remove(globVariablesInit.Length - 1, 1);
                globVariablesInit = globVariablesInit + "}";
            }

            Variable flowGlobalVariablesSchema = new Variable <string> {
                Name = "GlobalVariablesSchema", Default = globVariablesInit
            };

            Dictionary <int, FlowStep> nodeList = new Dictionary <int, System.Activities.Statements.FlowStep>();

            flow.Variables.Add(flowGlobalTransition);
            flow.Variables.Add(flowGlobalVariables);
            flow.Variables.Add(flowGlobalVariablesSchema);



            foreach (var state in subject.States)
            {
                FlowStep f;
                if (state.Type == PD_StateTypes.FunctionState)
                {
                    var    s       = (PD_FunctionState)state;
                    string timeout = "";
                    try
                    {
                        var timeouttransition = subject.Transitions.First(result => result.Source == s.Id && result.Type == PD_TransitionTypes.TimeoutTransition);

                        timeout = ((PD_TimeoutTransition)timeouttransition).TimeSpan;
                    }
                    catch (Exception e)
                    {
                    }
                    var           transitions = subject.Transitions.Where(result => result.Source == s.Id && result.Type == PD_TransitionTypes.RegularTransition);
                    List <string> titems      = new List <string>();
                    transitions.ToList().ForEach(i => titems.Add(((PD_RegularTransition)i).Name));
                    f = new FlowStep()
                    {
                        Action = new FunctionStateT()
                        {
                            DisplayName = s.Name, OrderId = s.Id, name = s.Name, GlobalTransition = new OutArgument <string>(flowGlobalTransition), GlobalVariables = new InOutArgument <DynamicValue>(flowGlobalVariables), isEndState = s.EndState, readableParameters = collectionToString(s.ReadableParameters), editableParameters = collectionToString(s.EditableParameters), TimeOut = timeout, transitions = collectionToString(titems)
                        }
                    };
                }
                else if (state.Type == PD_StateTypes.SendState)
                {
                    var    s       = (PD_SendState)state;
                    string timeout = "";
                    try
                    {
                        var timeouttransition = subject.Transitions.First(result => result.Source == s.Id && result.Type == PD_TransitionTypes.TimeoutTransition);

                        timeout = ((PD_TimeoutTransition)timeouttransition).TimeSpan;
                    }
                    catch (Exception e)
                    {
                    }
                    var    message = process.Messages.First(result => result.Id == s.Message);
                    string to      = process.Subjects.First(result => result.Id == message.To).Name;
                    f = new FlowStep()
                    {
                        Action = new SendStateT()
                        {
                            DisplayName = s.Name, OrderId = s.Id, name = s.Name, GlobalTransition = new OutArgument <string>(flowGlobalTransition), GlobalVariables = new InOutArgument <DynamicValue>(flowGlobalVariables), isEndState = s.EndState, readableParameters = collectionToString(s.ReadableParameters), editableParameters = collectionToString(s.EditableParameters), messageType = message.PD_MessageType.Name, parameters = collectionToString(message.PD_MessageType.Parameters), toSubject = to, TimeOut = timeout
                        }
                    };
                }
                else //(state.Type == PD_StateTypes.ReceiveState)
                {
                    var           s                  = (PD_ReceiveState)state;
                    string        messages           = "";
                    List <string> messagelist        = new List <string>();
                    var           receivetransitions = subject.Transitions.Where(result => result.Source == s.Id && result.Type == PD_TransitionTypes.ReceiveTransition);

                    foreach (var i in receivetransitions)
                    {
                        messagelist.Add(receiveTranstionToString(process, (PD_ReceiveTransition)i));
                    }
                    messages = collectionToString(messagelist);

                    string timeout = "";
                    try
                    {
                        var timeouttransition = subject.Transitions.First(result => result.Source == s.Id && result.Type == PD_TransitionTypes.TimeoutTransition);
                        timeout = ((PD_TimeoutTransition)timeouttransition).TimeSpan;
                    }
                    catch (Exception e)
                    {
                    }

                    f = new FlowStep()
                    {
                        Action = new ReceiveStateT()
                        {
                            DisplayName = s.Name, OrderId = s.Id, name = s.Name, GlobalTransition = new OutArgument <string>(flowGlobalTransition), GlobalVariables = new InOutArgument <DynamicValue>(flowGlobalVariables), isEndState = s.EndState, TimeOut = timeout, messages = messages
                        }
                    };
                }
                flow.Nodes.Add(f);
                nodeList.Add(state.Id, f);
            }


            var initGP = new FlowStep()
            {
                Action = new InitializeGlobalParameters()
                {
                    DisplayName = "init GP", DynamicVal = new InOutArgument <DynamicValue>(flowGlobalVariables), GlobalParameterSchema = new InArgument <string>(flowGlobalVariablesSchema)
                }
            };

            initGP.Next    = nodeList[subject.States.First(result => result.StartState == true).Id];
            flow.StartNode = initGP;

            // flow.StartNode = nodeList[subject.States.First(result => result.StartState == true).Id];


            foreach (var state in subject.States)
            {
                List <PD_Transition> transitions = new List <PD_Transition>();
                try
                {
                    subject.Transitions.Where(result => result.Source == state.Id).ToList().ForEach(item => transitions.Add(item));
                }
                catch (Exception e) { }

                if (transitions.Count > 0)
                {
                    if (transitions.Count == 1)
                    {
                        var t = transitions[0];
                        nodeList[t.Source].Next = nodeList[t.Target];
                    }
                    else
                    {
                        FlowSwitch <String> newSwitch = new FlowSwitch <String> {
                            Expression = flowGlobalTransition
                        };
                        flow.Nodes.Add(newSwitch);
                        nodeList[state.Id].Next = newSwitch;

                        try
                        {
                            var timeouttransition = transitions.First(result => result.Type == PD_TransitionTypes.TimeoutTransition);

                            newSwitch.Cases.Add("TimeOut!", nodeList[timeouttransition.Target]);
                            transitions.Remove(timeouttransition);
                        }
                        catch (Exception e) { }

                        if (state.Type == PD_StateTypes.SendState)
                        {
                            newSwitch.Default = nodeList[transitions[0].Target];
                        }
                        else if (state.Type == PD_StateTypes.ReceiveState)
                        {
                            foreach (var t in transitions)
                            {
                                newSwitch.Cases.Add(receiveTranstionToString(process, (PD_ReceiveTransition)t), nodeList[t.Target]);
                            }
                        }
                        else
                        {
                            foreach (var t in transitions)
                            {
                                newSwitch.Cases.Add(((PD_RegularTransition)t).Name, nodeList[t.Target]);
                            }
                        }
                    }
                }
            }

            ActivityBuilder builder = new ActivityBuilder();

            builder.Name = "strICT.InFlowTest.WFProcesses." + process.Name + "." + subject.Name;


            builder.Implementation = flow;

            VisualBasic.SetSettings(builder, new VisualBasicSettings());

            //StringBuilder sb = new StringBuilder();
            StringWriterUtf8 stream = new StringWriterUtf8();
            XamlWriter       writer = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(stream, new XamlSchemaContext()));

            XamlServices.Save(writer, builder);


            string res = stream.GetStringBuilder().ToString();

            res = res.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
            return(res);
        }