locked
Rehosted workflow designer 4.0 encoding ActivityXamlServices.Load RRS feed

  • Question

  • I have a rehosted workflow designer. The workflow definitions need to be saved into a database.

    You can get the xaml definition from the workflowdesigner using WorkflowDesigner.Text (after flush)

    I need to put the xaml definition into a byte[] so I used : Encoding.Default.GetBytes().

    But when I use ActivityXamlServices.Load(stream); to validate or load the workflow, I get an error Invalid character.

    I also tried Encoding.Convert(new ASCIIEncoding(), new UTF8Encoding(), Encoding.ASCII.GetBytes(_workflowDesigner.Text))
    then the workflow can be loaded without any problem with ActivityXamlServices.Load(stream); but foreign characters are not correct...

    Saturday, May 12, 2012 10:34 AM

Answers

  • You shouldn't use Encoding.Default to save XAML. Since XAML is an XML format, the default encoding used for a byte stream should be the default XML coding (if no byte order mark is included), which is UTF-8.

    I would recommend you try byte[] = Encoding.UTF8.GetBytes(WorkflowDesigner.Text).

    Note that Encoding.Default returns a different ANSI encoding depending on which code page you have as default on your machine, and therefore won't deserialize correctly on other machines, so usually it's a bad idea to use it for storing data in modern day applications where data might get transferred between machines.

    Tim

    • Proposed as answer by Tim Lovell-Smith Sunday, May 13, 2012 7:15 AM
    • Marked as answer by LeoTang Friday, May 18, 2012 11:36 AM
    Sunday, May 13, 2012 7:15 AM

All replies

  • Foreign character are unicode characters which are 16 bit instead of the normal 8 bit ascii.  So foreign chanters will look like binary data and not all string methods will work with binary data.  encoding a Decoding the data the way you are doing is the correct method of handling binary type data. Encoding the data will modify unicode characters so you don't want to use unicode.

    There are issues with using streams with binary data.  So validating with a stream method may not work.  The webpage below discusses some issues with the generic stream reader (some of the issues are the same as yours because of the binary nature of your data).

    http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/90d4d7b0-6d99-4464-84a7-fcb6201f9937

    I would try using Binarywriter and Binaryreader.  Also make sure the database is setupo to handle the binary data.

    http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx

    http://msdn.microsoft.com/en-us/library/system.io.binaryreader


    jdweng

    Saturday, May 12, 2012 12:24 PM
  • You shouldn't use Encoding.Default to save XAML. Since XAML is an XML format, the default encoding used for a byte stream should be the default XML coding (if no byte order mark is included), which is UTF-8.

    I would recommend you try byte[] = Encoding.UTF8.GetBytes(WorkflowDesigner.Text).

    Note that Encoding.Default returns a different ANSI encoding depending on which code page you have as default on your machine, and therefore won't deserialize correctly on other machines, so usually it's a bad idea to use it for storing data in modern day applications where data might get transferred between machines.

    Tim

    • Proposed as answer by Tim Lovell-Smith Sunday, May 13, 2012 7:15 AM
    • Marked as answer by LeoTang Friday, May 18, 2012 11:36 AM
    Sunday, May 13, 2012 7:15 AM
  • Hi Rekna,

    Could you please share the code for rehosting WWF from database? I tried the below method but its not loading.

    var stream = ReadWorkflowFromDB();

    var workflow = ActivityXamlServices.Load(stream);
                MainWindow.AddAssemblies(workflow);
                _workflowDesigner.Load(workflow);
    Thanks in advance

    www.joyfulwpf.blogspot.com , www.silverlightedweb.blogspot.com, www.joymonscode.blogspot.com

    Monday, July 28, 2014 6:42 PM