locked
Inserting a new xaml node into a xaml file RRS feed

  • Question

  • Hello Everyone,

    Let's say I have a xaml file such as:
    <?xml version="1.0" encoding="utf-8" ?>
    <p:Activity
              xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"
              xmlns:s="clr-namespace:System;assembly=mscorlib"
              xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"
              xmlns:sadx="clr-namespace:System.Activities.Design.Xaml;assembly=System.Activities.Design"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        >
        <p:Sequence sad:XamlDebuggerXmlReader.FileName="C:\Users\Bob\Documents\Visual Studio 10\Projects\XAMLNodeLoop\WorkflowConsoleApplication1\Sequence1.xaml" 
    x:Name="sequence1"> </p:Sequence> </p:Activity>
    Now I want to programmatically insert a WriteLine activity into the sequence activity node with the Text Expression in the Writeline activity being DateTime.Now.  I would think this would be a simple operation, but I just can't seem to adapt what I'm finding on various blogs to perform this function.  I'm getting weird exceptions, but I would rather know how the pros would go about this instead of just fixing my particular attempt at it.

    Thanks in advance,

    Bob
    Saturday, June 27, 2009 11:35 PM

Answers

  • Bob, sorry, I didn't see your response. 

    We don't have a formal programming model against the xaml, we work to edit the instance and serialization is something that is left up to the host (you may want to write to a database or do a different format).  Rather, you can program against the model item tree offered by the designer and serialize that, or you could just build the object imperatively at the right spot and then use the xaml serialization in order to get the xaml output you are looking for.
    Program Manager -- wf designer -- http://blogs.msdn.com/mwinkle
    Monday, July 13, 2009 11:20 PM

All replies

  • Any suggestions on how I should proceed at a high level at least?
    Tuesday, June 30, 2009 3:18 AM
  • Bob, what do you mean by programmatically insert  a writeline.  Is this in the designer, programming against the xaml API, etc? If you could post the code of what you're trying to do, I'd appreciate it!

    Thanks,

    matt
    Program Manager -- Modeling Platform and Tools http://blogs.msdn.com/mwinkle
    Wednesday, July 1, 2009 10:08 PM
  • If I opened the xaml file I included above and opened it in the designer it would show a valid activity containing a sequence activity in it (granted not very useful at this point).  Now using the designer I could drop a writeline activity on the sequence activity and assign the expression as DateTime.Now.ToString() which would give me the following xaml. 

    <p:Activity 
      mc:Ignorable="" 
      x:Class="TestSequentialWorkflow.Sequence1" 
      xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design" 
      xmlns:__Sequence1="clr-namespace:TestSequentialWorkflow;" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities" 
      xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      >
      <p:Sequence sad:XamlDebuggerXmlReader.FileName="C:\Users\Bob\Desktop\Amadeus\Reflector\TestSequentialWorkflow\Sequence1.xaml" x:Name="sequence1">
        <p:WriteLine>[DateTime.Now.ToString()]</p:WriteLine>
      </p:Sequence>
    </p:Activity>
    Basically, I was wondering how I could get to the exact same xaml by inserting the Writeline node programmatically.  It would be cool if I had access to the same logic the designer uses without needing the UI.  Like calling a method such as:

    DropActivity("MyActivity.xaml", WriteLine, "sequence1");

    Calling my imaginary method DropActivity would do everything the designer did when I dropped it on the sequence activity.  The first parameter is the xaml file we want to work with, the second is the activity to drop, the third is the name of the activity to drop it into.  This method should do all the other stuff the designer does such as adding the proper namespaces and the other housekeeping xaml without the designer UI overhead.  I wish there was such a thing as linq to xaml, which would probably make it much easier to build workflows programmatically.  Anyhow, how would you recommed I get from the xaml in my initial question to the xaml I posted here?  I realize I could use linq to xml, but I would like any necessary namespaces and whatever else that's needed to go along with an activity insertion like the designer will do.

    Thanks in advance,

    Bob
    Thursday, July 2, 2009 12:53 AM
  • So is there no easy way to go about what I would like to do?  How about a high level overview of the hard way?

    Thanks in advance,

    Bob
    Tuesday, July 7, 2009 12:05 AM
  • Hi Bob,
    Just for clarification are you wanting to do this dynamically at runtime? The DynamicActivity should provide want you need if you are looking for a runtime solution. Otherwise you're going to need to look into the XAML APIs.

    Thanks,
    Amy
    Monday, July 13, 2009 10:49 PM
  • Bob, sorry, I didn't see your response. 

    We don't have a formal programming model against the xaml, we work to edit the instance and serialization is something that is left up to the host (you may want to write to a database or do a different format).  Rather, you can program against the model item tree offered by the designer and serialize that, or you could just build the object imperatively at the right spot and then use the xaml serialization in order to get the xaml output you are looking for.
    Program Manager -- wf designer -- http://blogs.msdn.com/mwinkle
    Monday, July 13, 2009 11:20 PM
  • Actually, no.  I just want to build and modify xaml programmatically before it is run.  Insert and delete activities, etc.  I have been working with a sample rob relyea provided on his blog.  Its coming along, but the xaml api just aren't very intuitive.

    Bob
    Monday, July 13, 2009 11:27 PM
  • Thanks anyhow Matt.  I've got it pretty much figured out with the Xaml api, its just going to take a while to work out the code to get the flexibility I need.

    Thanks,

    Bob
    Monday, July 13, 2009 11:28 PM