Simpler WF Rules Serializer

I have been looking at the Windows WF Rules serialization.  The default version creates 100 lines of illegible xml for each line of entered rules.  My version creates 10 lines per line of rule.  I am tempted to incorporate this into the stand-alone ruleset editor. My version would initially store the readable version alongside the compiled version.

Here is a version that creates cleaner XML:

    public class TextRuleSetSerializer
    {
        public void Serialize(XmlWriter writer, RuleSet rs)
        {
            writer.WriteStartDocument();
            writer.WriteStartElement(“RuleSet.Text”);
            writer.WriteAttributeString(“Name”, rs.Name);
            writer.WriteAttributeString(“Description”, rs.Description);
            writer.WriteAttributeString(“ChainingBehaviour”, rs.ChainingBehavior.ToString());
                       
            foreach (var rule in rs.Rules)
            {
                writer.WriteStartElement(“Rule”);
                writer.WriteAttributeString(“Active”, rule.Active.ToString());
                writer.WriteAttributeString(“Description”, rule.Description);
                writer.WriteAttributeString(“Name”, rule.Name);
                writer.WriteAttributeString(“Priority”, rule.Priority.ToString());
                writer.WriteAttributeString(“ReevaluationBehavior”, rule.ReevaluationBehavior.ToString());

                writer.WriteStartElement(“Condition”);
                writer.WriteAttributeString(“ConditionType”, rule.Condition.GetType().FullName);
                writer.WriteAttributeString(“ConditionAssembly”, rule.Condition.GetType().AssemblyQualifiedName);
                writer.WriteValue(rule.Condition.ToString());
                writer.WriteEndElement();

                writer.WriteStartElement(“ThenActions”);
               
                foreach(var action in rule.ThenActions)
                {
                    writer.WriteStartElement(“Action”);
                    writer.WriteAttributeString(“ActionType”, action.GetType().FullName);
                    writer.WriteAttributeString(“ActionAssembly”, action.GetType().AssemblyQualifiedName);
                    writer.WriteValue(action.ToString());
                    writer.WriteEndElement();
                }
               
                writer.WriteEndElement();
               
                writer.WriteStartElement(“ElseActions”);

                foreach (var action in rule.ElseActions)
                {
                    writer.WriteStartElement(“Action”);
                    writer.WriteAttributeString(“ActionType”, action.GetType().FullName);
                    writer.WriteAttributeString(“ActionAssembly”, action.GetType().AssemblyQualifiedName);
                    writer.WriteValue(action.ToString());
                    writer.WriteEndElement();
                }
               
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
     
            writer.Flush();
        }
}

I am still working on the deserialization version.  This is more difficult since the code that is used in the RuleSetDialog to compile the entered text into CodeDOM is an internal class.  This leaves me with two options: 

  1. Use reflector to create a non-internal version of the same code.
  2. Puppeteer the RuleSetDialog to perform the compilation for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s