/// <summary>
        /// Serializes expressions to an xml string.
        /// </summary>
        /// <param name="container">An instance of <see cref="ExpressionContainer"/> to serialize.</param>
        /// <returns><see cref="System.String"/></returns>
        public override string Serialize(ExpressionContainer container)
        {
            var textWriter = new StringWriter();

            using (var xmlWriter = XmlWriter.Create(textWriter, new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }))
            {
                xmlWriter.WriteStartDocument(true);
                xmlWriter.WriteStartElement("Expressions");
                xmlWriter.WriteAttributeString("version", XmlVersion);
                xmlWriter.WriteAttributeString("type", container.Type.ToString());

                if (container.Type == ExpressionContainerType.DesignerBased)
                {
                    foreach (var obj in container.Expressions)
                    {
                        xmlWriter.WriteStartElement("Expression");
                        obj.Serialize(xmlWriter);
                        xmlWriter.WriteEndElement();
                    }
                }
                else
                {
                    xmlWriter.WriteCData(container.ExpressionScript);
                }

                xmlWriter.WriteEndDocument();
            }

            return textWriter.ToString();
        }
        public void TestScriptSerialization()
        {
            var expr = new ExpressionContainer("script");
            var serializer = new XmlExpressionsSerializer();
            var xml = serializer.Serialize(expr);

            var result = serializer.Deserialize(xml);
            Assert.IsNotNull(result);
            Assert.AreEqual(expr.ExpressionScript, result.ExpressionScript);
        }
        public void TestSerialization()
        {
            var expr =
                new ExpressionContainer(
                    new IExpressionObjectBase[]
                        {
                            new Connection { Source = new ConnectorOut(null), Sink = new ConnectorIn(null) }, new ConstantExpression(),
                            new OneInOneOutExpression(ExpressionNodeType.ToLower)
                        });
            var serializer = new XmlExpressionsSerializer();
            var xml = serializer.Serialize(expr);

            var result = serializer.Deserialize(xml);
            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Expressions.Count);
            Assert.IsTrue(result.Expressions.Any(x => x is Connection));
            Assert.IsTrue(result.Expressions.Any(x => x is ConstantExpression));
            Assert.IsTrue(result.Expressions.Any(x => x is OneInOneOutExpression));
        }
        /// <summary>
        /// Deserializes expressions.
        /// </summary>
        /// <param name="xml">Serialization string.</param>
        /// <returns><see cref="ExpressionContainer"/></returns>
        public override ExpressionContainer Deserialize(string xml)
        {
            if (string.IsNullOrEmpty(xml))
                return new ExpressionContainer(new List<IExpressionObjectBase>());

            var textReader = new StringReader(xml);

            using (var xmlReader = XmlReader.Create(textReader))
            {
                xmlReader.ReadToFollowing("Expressions");
                var ver = xmlReader.GetAttribute("version");
                var type = xmlReader.GetAttribute("type");

                if (ver != XmlVersion)
                    throw new Exception("Wrong xml version. Expected " + XmlVersion + " but was " + ver);

                ExpressionContainerType containerType;
                if (!Enum.TryParse(type, out containerType))
                    throw new Exception("Wrong container type");

                if (containerType == ExpressionContainerType.ScriptBased)
                    return new ExpressionContainer(xmlReader.ReadElementContentAsString());

                var result = new ExpressionContainer(new List<IExpressionObjectBase>());

                while (xmlReader.Read())
                {
                    if (xmlReader.NodeType != XmlNodeType.Element || !"Expression".Equals(xmlReader.Name))
                        continue;

                    var exprType = xmlReader.GetAttribute("type");
                    ExpressionNodeType nodeType;
                    if (!Enum.TryParse(exprType, out nodeType))
                        throw new Exception("Wrong expression node type: " + type);

                    var expression = CreateExpressionByType(nodeType);
                    expression.Deserialize(xmlReader);
                    result.Expressions.Add(expression);
                }

                return result;
            }
        }
 /// <summary>
 /// Serializes the expressions container to a deserializable string.
 /// </summary>
 /// <param name="expression">An instance of <see cref="ExpressionContainer"/> to serialize.</param>
 /// <returns><see cref="System.String"/></returns>
 public abstract string Serialize(ExpressionContainer expression);