The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

Saving a workflow to a XOML file.

Sometimes you just want to be able to create a workflow in code bus save it as a XOML workflow so the end user has the opportunity to modify it.

Well it turns out to be quite easy Smile as the WorkflowMarkupSerializer class is your friend. The following code does just that. And most of the code is to create the workflow in the first place as the serialization part is just 3 lines!

using (XmlWriter writer = XmlWriter.Create("MyWorkflow.xoml"))
{
    WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
    SequentialWorkflowActivity myWorkflow = new SequentialWorkflowActivity();

    ListenActivity listen = new ListenActivity();
    myWorkflow.Activities.Add(listen);
    EventDrivenActivity leftBranch = new EventDrivenActivity();
    leftBranch.Activities.Add(new DelayActivity("LeftDelay")
    {
        TimeoutDuration = TimeSpan.FromSeconds(5)
    });
    listen.Activities.Add(leftBranch);

    EventDrivenActivity rightBranch = new EventDrivenActivity();
    rightBranch.Activities.Add(new DelayActivity("RightDelay")
    {
        TimeoutDuration = TimeSpan.FromSeconds(5)
    });
    listen.Activities.Add(rightBranch);

    serializer.Serialize(writer, myWorkflow);
}

This produces the following XOML workflow:

<?xml version="1.0" encoding="utf-8"?>
<SequentialWorkflowActivity x:Name="SequentialWorkflowActivity"
                            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
  <ListenActivity x:Name="listenActivity1">
    <EventDrivenActivity x:Name="eventDrivenActivity1">
      <DelayActivity TimeoutDuration="00:00:05" x:Name="LeftDelay" />
    </EventDrivenActivity>
    <EventDrivenActivity x:Name="eventDrivenActivity2">
      <DelayActivity TimeoutDuration="00:00:05" x:Name="RightDelay" />
    </EventDrivenActivity>
  </ListenActivity>
</SequentialWorkflowActivity>

image

Enjoy!

www.TheProblemSolver.nl
http://wiki.WindowsWorkflowFoundation.eu

Published Wed, Apr 9 2008 10:21 by Maurice
Filed under: , , ,

Comments

# re: Saving a workflow to a XOML file.@ Sunday, November 09, 2008 6:47 PM

If I want to add  the ServiceInputActivity and ServiceOutputActivity to the StateMachine Worlflow ,How can I do?

by flyingingBird