locked
How to serialize an activity to xamlx (WorkflowService) RRS feed

  • Question

  • Hi,

    I need to serialize an activity to to xamlx,
    I can use the ActivityXamlServices to obtain the xaml representation but how about xamlx ?

    Thanks in advance for your help.
    --
    S. Jacobi
    Wednesday, May 12, 2010 10:33 AM

Answers

  • Hi,

    I think you can simply use the XamlServices. See the following example.

                WorkflowService service = new WorkflowService()
                    {
                        Body = new Sequence()
                    };
                using (StringWriter textWriter = new StringWriter())
                {
                   XamlServices.Save(textWriter, service);
                   System.Console.WriteLine(textWriter.ToString());
                }

    The reason why we need ActivityXamlServices to serialize a new type of Activity is that we need to rewrite "<ActivityBuilder" to "<Activity x:Class". WorkflowService doesn't have that problem.

    Does that answer your questions?

    Thanks,
    Kenny

    • Marked as answer by S.Jacobi Wednesday, May 12, 2010 3:05 PM
    Wednesday, May 12, 2010 1:00 PM

All replies

  • Hi,

    I think you can simply use the XamlServices. See the following example.

                WorkflowService service = new WorkflowService()
                    {
                        Body = new Sequence()
                    };
                using (StringWriter textWriter = new StringWriter())
                {
                   XamlServices.Save(textWriter, service);
                   System.Console.WriteLine(textWriter.ToString());
                }

    The reason why we need ActivityXamlServices to serialize a new type of Activity is that we need to rewrite "<ActivityBuilder" to "<Activity x:Class". WorkflowService doesn't have that problem.

    Does that answer your questions?

    Thanks,
    Kenny

    • Marked as answer by S.Jacobi Wednesday, May 12, 2010 3:05 PM
    Wednesday, May 12, 2010 1:00 PM
  • Hi,

    Here is some additional information on serializing activities and workflows to and from Xaml:

    Serializing Workflows and Activities to and from XAML

    Here is an example:

    ActivityBuilder<int> ab = new ActivityBuilder<int>();
    ab.Name = "Add";
    ab.Properties.Add(new DynamicActivityProperty { Name = "Operand1", Type = typeof(InArgument<int>) });
    ab.Properties.Add(new DynamicActivityProperty { Name = "Operand2", Type = typeof(InArgument<int>) });
    ab.Implementation = new Sequence
    {
      Activities = 
      {
        new WriteLine
        {
          Text = new VisualBasicValue<string> 
          {
            ExpressionText= "Operand1.ToString() + \" + \" + Operand2.ToString()"
          },
        },
        new Assign<int>
        {
          To = new ArgumentReference<int> { ArgumentName = "Result" },
          Value = new VisualBasicValue<int>
          {
            ExpressionText = "Operand1 + Operand2"
          }
        }
      }
    };
    
    // Serialize the workflow to XAML and store it in a string.
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    XamlWriter xw = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(tw, new XamlSchemaContext()));
    XamlServices.Save(xw , ab);
    string serializedAB = sb.ToString();
    
    // Display the XAML to the console.
    Console.WriteLine(serializedAB);
    
    // Serialize the workflow to XAML and save it to a file.
    StreamWriter sw = File.CreateText(@"C:\Workflows\add.xaml");
    XamlWriter xw2 = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(sw, new XamlSchemaContext()));
    XamlServices.Save(xw2, ab);
    sw.Close();

    This yields the following Xaml:

    <Activity 
     x:TypeArguments="x:Int32" 
     x:Class="Add" 
     xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <x:Members>
      <x:Property Name="Operand1" Type="InArgument(x:Int32)" />
      <x:Property Name="Operand2" Type="InArgument(x:Int32)" />
     </x:Members>
     <Sequence>
      <WriteLine Text="[Operand1.ToString() + &quot; + &quot; + Operand2.ToString()]" />
      <Assign x:TypeArguments="x:Int32" Value="[Operand1 + Operand2]">
       <Assign.To>
        <OutArgument x:TypeArguments="x:Int32">
         <ArgumentReference x:TypeArguments="x:Int32" ArgumentName="Result" />
         </OutArgument>
       </Assign.To>
       </Assign>
     </Sequence>
    </Activity>

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

     

    Wednesday, May 12, 2010 2:46 PM
  • Thanks,
    Your posts are very helpful.  :)
    Wednesday, May 12, 2010 3:07 PM