Esempio n. 1
0
        private void SerializeIEnumerableObject(XElement parent, IEnumerable graph, XmlSerializeContext context)
        {
            //此处对一维数组与list<类型>的处理,其它的都以base64string处理
            Type objectType = graph.GetType();

            if (objectType.IsArray)
            {
                (objectType.GetArrayRank() == 1).FalseThrow <InvalidOperationException>("{0}不是一维数组,我们只支持一维数组", objectType.FullName);

                int rank = GetArrayRank(objectType);
                if (rank == 1)
                {
                    parent.SetAttributeValue("dimLength", GetArrayDimensionsLength(objectType.GetArrayRank(), (Array)graph));
                    SerializeIEnumerableOjbectToNode(graph, context, parent);
                }
                else
                {
                    //嵌套数组
                    parent.AddChildElement("Value", SerializationHelper.SerializeObjectToString(graph, SerializationFormatterType.Binary));
                }
            }
            else
            {
                SerializeIEnumerableOjbectToNode(graph, context, parent);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 对象序列化为XElement
        /// </summary>
        /// <param name="graph"></param>
        /// <returns></returns>
        public XElement Serialize(object graph)
        {
            XElementFormatter.FormattingStatus = XElementFormattingStatus.Serializing;

            try
            {
                XElement root = XElement.Parse("<Root/>");

                XmlSerializeContext context = new XmlSerializeContext()
                {
                    RootElement = root, ContainerTypeName = graph.GetType().AssemblyQualifiedName
                };

                int objID = context.CurrentID;
                context.ObjectContext.Add(graph, objID);

                XElement graphElement = SerializeObjectToNode(root, graph, context);

                root.SetAttributeValue("v", objID);
                root.SetAttributeValue("r", true);

                context.SerializeTypeInfo(root);

                return(root);
            }
            finally
            {
                XElementFormatter.FormattingStatus = XElementFormattingStatus.None;
            }
        }
Esempio n. 3
0
        private void SerializePropertiesToNodes(XElement parent, object graph, XmlSerializeContext context)
        {
            TypeFields tf = TypeFields.GetTypeFields(graph.GetType());

            foreach (KeyValuePair <TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields)
            {
                ExtendedFieldInfo efi = kp.Value;

                if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi))
                {
                    object data = GetValueFromObject(efi.FieldInfo, graph);

                    if ((data == null || data == DBNull.Value) == false)
                    {
                        if (Type.GetTypeCode(data.GetType()) == TypeCode.Object)
                        {
                            int objID = 0;

                            if (context.ObjectContext.TryGetValue(data, out objID) == false)
                            {
                                objID = context.CurrentID;
                                context.ObjectContext.Add(data, objID);
                                context.ContainerTypeName = graph.GetType().AssemblyQualifiedName + "." + efi.FieldInfo.Name;

                                SerializeObjectToNode(parent, data, context);
                            }

                            XElement propertyElem = parent.AddChildElement("F");

                            propertyElem.SetAttributeValue("n", efi.AlternateFieldName);
                            propertyElem.SetAttributeValue("v", objID);
                            propertyElem.SetAttributeValue("r", true);
                            propertyElem.SetAttributeValue("oti", context.GetTypeID(kp.Key.ObjectType));

                            if (efi.IgnoreDeserializeError)
                            {
                                propertyElem.SetAttributeValue("ide", efi.IgnoreDeserializeError);
                            }
                        }
                        else
                        {
                            XElement propertyElem = parent.AddChildElement("F");
                            propertyElem.SetAttributeValue("n", efi.AlternateFieldName);
                            propertyElem.SetAttributeValue("v", data);
                            propertyElem.SetAttributeValue("oti", context.GetTypeID(kp.Key.ObjectType));

                            if (efi.IgnoreDeserializeError)
                            {
                                propertyElem.SetAttributeValue("ide", efi.IgnoreDeserializeError);
                            }
                        }
                    }
                }
            }
        }
		public void Serialize(XElement node, XmlSerializeContext context)
		{
			if (this.Name.IsNotEmpty())
				node.SetAttributeValue("_na", this.Name);

			if (this.DisplayName.IsNotEmpty())
				node.SetAttributeValue("_dn", this.DisplayName);

			if (this.Columns > 0)
				node.SetAttributeValue("_columns", this.Columns);

			if (this.DefaultRowHeight.IsNotEmpty())
				node.SetAttributeValue("_drh", this.DefaultRowHeight);
		}
Esempio n. 5
0
        private void SerializeIEnumerableOjbectToNode(IEnumerable graph, XmlSerializeContext context, XElement xElement)
        {
            XElement itemsNode = xElement.AddChildElement("Items");

            foreach (object data in graph)
            {
                if (data != null)
                {
                    XElement itemNode = itemsNode.AddChildElement("I");

                    if (Type.GetTypeCode(data.GetType()) != TypeCode.Object)
                    {
                        itemNode.SetAttributeValue("v", data);

                        if (this.OutputShortType)
                        {
                            itemNode.SetAttributeValue("shortType", data.GetType().Name);
                        }

                        itemNode.SetAttributeValue("oti", context.GetTypeID(data.GetType()));
                    }
                    else
                    {
                        int objID = 0;

                        if (context.ObjectContext.TryGetValue(data, out objID) == false)
                        {
                            objID = context.CurrentID;
                            context.ObjectContext.Add(data, objID);
                            context.ContainerTypeName = graph.GetType().AssemblyQualifiedName;
                            SerializeObjectToNode(xElement, data, context);
                        }

                        itemNode.SetAttributeValue("v", objID);

                        if (this.OutputShortType)
                        {
                            itemNode.SetAttributeValue("shortType", data.GetType().Name);
                        }

                        itemNode.SetAttributeValue("oti", context.GetTypeID(data.GetType()));
                        itemNode.SetAttributeValue("r", true);
                    }
                }
            }
        }
Esempio n. 6
0
        public void Serialize(XElement node, XmlSerializeContext context)
        {
            PropertyLayout pl = this.Clone();
            int objID = 0;

            if (context.ObjectContext.TryGetValue(pl, out objID) == false)
            {
                objID = context.CurrentID;
                context.ObjectContext.Add(pl, objID);
                node.SetAttributeValue("id", context.CurrentID++);
                pl.LayoutSection.Serialize(node, context);
            }
            else
            {
                node.SetAttributeValue("v", objID);
            }
        }
Esempio n. 7
0
        private XElement SerializeObjectToNode(XElement parent, object graph, XmlSerializeContext context)
        {
            System.Type type = graph.GetType();

            type.IsSerializable.FalseThrow <SerializationException>("XElementFormatter错误,序列化对象类型时{0}, 子类型{1}不可序列化",
                                                                    context.ContainerTypeName, type.AssemblyQualifiedName);

            XElement node  = context.RootElement.AddChildElement("O");
            int      objID = context.CurrentID++;

            node.SetAttributeValue("id", objID);

            if (this.OutputShortType)
            {
                node.SetAttributeValue("shortType", type.Name);
            }

            node.SetAttributeValue("oti", context.GetTypeID(type));

            if (graph is IXElementSerializable)
            {
                node.SetAttributeValue("cs", "true");
                ((IXElementSerializable)graph).Serialize(node, context);
            }
            else
            {
                if (TypeFields.GetTypeFields(type).XElementSerializable)
                {
                    if (graph is IEnumerable)
                    {
                        SerializeIEnumerableObject(node, (IEnumerable)graph, context);
                    }

                    SerializePropertiesToNodes(node, graph, context);
                }
                else
                {
                    node.AddChildElement("Value", SerializationHelper.SerializeObjectToString(graph, SerializationFormatterType.Binary));
                }
            }

            return(node);
        }
		public void PropertyValueSerializeTest()
		{
			PropertyValue pv = PreparePropertyValue();

			XElementFormatter formatter = new XElementFormatter();
			XmlSerializeContext context = new XmlSerializeContext();
			XElement root = new XElement("root");
			pv.Serialize(root, context);

			Console.WriteLine(root.ToString());

			XmlDeserializeContext dcontext = new XmlDeserializeContext();
			PropertyValue newPropertyValue = new PropertyValue(new PropertyDefine());
			newPropertyValue.Deserialize(root, dcontext);

			//Assert.AreEqual(root.ToString(), rootReserialized.ToString());
			Assert.AreEqual(pv.StringValue, newPropertyValue.StringValue);
			Assert.AreEqual(pv.Definition.Name, newPropertyValue.Definition.Name);
		}
Esempio n. 9
0
        /// <summary>
        /// 为通用的序列化而显示实现接口IXElementSerializable
        /// </summary>
        /// <param name="node"></param>
        /// <param name="context"></param>
        void IXElementSerializable.Serialize(XElement node, XmlSerializeContext context)
        {
            if (this.Definition.DefaultValue.IsNullOrEmpty())
                this.Definition.DefaultValue = string.Empty;

            if (this.StringValue.IsNullOrEmpty())
                this.StringValue = string.Empty;

            this.Definition.Serialize(node, context);

            if (!this.StringValue.IsNullOrEmpty() && this.Definition.DefaultValue.ToLower() != this.StringValue.ToLower())
                node.SetAttributeValue("_sv", this.StringValue);
        }
Esempio n. 10
0
        private void SerializableBinaryToNode(XElement parent, XmlObjectMappingItem item, object graph, XmlSerializeContext context)
        {
            //此处对没有标记为序列化的需要做一个异常处理,还是?
            XElement xElement = context.RootElement.AddChildElement("object");
            xElement.SetAttributeValue("id", context.CurrentID++);

            if (this.OutputShortType)
                xElement.SetAttributeValue("shortType", graph.GetType().Name);

            xElement.SetAttributeValue("oti", context.GetTypeID(graph.GetType()));

            XElement itemNode = xElement.AddChildElement("value");

            byte[] buffer = null;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, graph);
                buffer = ms.GetBuffer();
            }
            string imageBase64String = Convert.ToBase64String(buffer);

            itemNode.Add(imageBase64String);
        }
Esempio n. 11
0
        private void SerializePropertiesToNodes(XElement parent, object graph, XmlSerializeContext context)
        {
            TypeFields tf = TypeFields.GetTypeFields(graph.GetType());

            foreach (KeyValuePair<TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields)
            {
                ExtendedFieldInfo efi = kp.Value;

                if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi))
                {
                    object data = GetValueFromObject(efi.FieldInfo, graph);

                    if ((data == null || data == DBNull.Value) == false)
                    {
                        if (Type.GetTypeCode(data.GetType()) == TypeCode.Object)
                        {
                            int objID = 0;

                            if (context.ObjectContext.TryGetValue(data, out objID) == false)
                            {
                                objID = context.CurrentID;
                                context.ObjectContext.Add(data, objID);
                                context.ContainerTypeName = graph.GetType().AssemblyQualifiedName + "." + efi.FieldInfo.Name;

                                SerializeObjectToNode(parent, data, context);
                            }

                            XElement propertyElem = parent.AddChildElement("F");

                            propertyElem.SetAttributeValue("n", efi.AlternateFieldName);
                            propertyElem.SetAttributeValue("v", objID);
                            propertyElem.SetAttributeValue("r", true);
                            propertyElem.SetAttributeValue("oti", context.GetTypeID(kp.Key.ObjectType));

                            if (efi.IgnoreDeserializeError)
                                propertyElem.SetAttributeValue("ide", efi.IgnoreDeserializeError);
                        }
                        else
                        {
                            XElement propertyElem = parent.AddChildElement("F");
                            propertyElem.SetAttributeValue("n", efi.AlternateFieldName);
                            propertyElem.SetAttributeValue("v", data);
                            propertyElem.SetAttributeValue("oti", context.GetTypeID(kp.Key.ObjectType));

                            if (efi.IgnoreDeserializeError)
                                propertyElem.SetAttributeValue("ide", efi.IgnoreDeserializeError);
                        }
                    }
                }
            }
        }
Esempio n. 12
0
        private void SerializeIEnumerableOjbectToNode(IEnumerable graph, XmlSerializeContext context, XElement xElement)
        {
            XElement itemsNode = xElement.AddChildElement("Items");

            foreach (object data in graph)
            {
                if (data != null)
                {
                    XElement itemNode = itemsNode.AddChildElement("I");

                    if (Type.GetTypeCode(data.GetType()) != TypeCode.Object)
                    {
                        itemNode.SetAttributeValue("v", data);

                        if (this.OutputShortType)
                            itemNode.SetAttributeValue("shortType", data.GetType().Name);

                        itemNode.SetAttributeValue("oti", context.GetTypeID(data.GetType()));
                    }
                    else
                    {
                        int objID = 0;

                        if (context.ObjectContext.TryGetValue(data, out objID) == false)
                        {
                            objID = context.CurrentID;
                            context.ObjectContext.Add(data, objID);
                            context.ContainerTypeName = graph.GetType().AssemblyQualifiedName;
                            SerializeObjectToNode(xElement, data, context);
                        }

                        itemNode.SetAttributeValue("v", objID);

                        if (this.OutputShortType)
                            itemNode.SetAttributeValue("shortType", data.GetType().Name);

                        itemNode.SetAttributeValue("oti", context.GetTypeID(data.GetType()));
                        itemNode.SetAttributeValue("r", true);
                    }
                }
            }
        }
Esempio n. 13
0
        private void SerializeIEnumerableObject(XElement parent, IEnumerable graph, XmlSerializeContext context)
        {
            //此处对一维数组与list<类型>的处理,其它的都以base64string处理
            Type objectType = graph.GetType();

            if (objectType.IsArray)
            {
                (objectType.GetArrayRank() == 1).FalseThrow<InvalidOperationException>("{0}不是一维数组,我们只支持一维数组", objectType.FullName);

                int rank = GetArrayRank(objectType);
                if (rank == 1)
                {
                    parent.SetAttributeValue("dimLength", GetArrayDimensionsLength(objectType.GetArrayRank(), (Array)graph));
                    SerializeIEnumerableOjbectToNode(graph, context, parent);
                }
                else
                {
                    //嵌套数组
                    parent.AddChildElement("Value", SerializationHelper.SerializeObjectToString(graph, SerializationFormatterType.Binary));
                }
            }
            else
            {
                SerializeIEnumerableOjbectToNode(graph, context, parent);
            }
        }
Esempio n. 14
0
        private XElement SerializeObjectToNode(XElement parent, object graph, XmlSerializeContext context)
        {
            System.Type type = graph.GetType();

            type.IsSerializable.FalseThrow<SerializationException>("XElementFormatter错误,序列化对象类型时{0}, 子类型{1}不可序列化",
                context.ContainerTypeName, type.AssemblyQualifiedName);

            XElement node = context.RootElement.AddChildElement("O");
            int objID = context.CurrentID++;

            node.SetAttributeValue("id", objID);

            if (this.OutputShortType)
                node.SetAttributeValue("shortType", type.Name);

            node.SetAttributeValue("oti", context.GetTypeID(type));

            if (graph is IXElementSerializable)
            {
                node.SetAttributeValue("cs", "true");
                ((IXElementSerializable)graph).Serialize(node, context);
            }
            else
            {
                if (TypeFields.GetTypeFields(type).XElementSerializable)
                {
                    if (graph is IEnumerable)
                        SerializeIEnumerableObject(node, (IEnumerable)graph, context);

                    SerializePropertiesToNodes(node, graph, context);
                }
                else
                {
                    node.AddChildElement("Value", SerializationHelper.SerializeObjectToString(graph, SerializationFormatterType.Binary));
                }
            }

            return node;
        }
Esempio n. 15
0
        /// <summary>
        /// 对象序列化为XElement
        /// </summary>
        /// <param name="graph"></param>
        /// <returns></returns>
        public XElement Serialize(object graph)
        {
            XElementFormatter.FormattingStatus = XElementFormattingStatus.Serializing;

            try
            {
                XElement root = XElement.Parse("<Root/>");

                XmlSerializeContext context = new XmlSerializeContext() { RootElement = root, ContainerTypeName = graph.GetType().AssemblyQualifiedName };

                int objID = context.CurrentID;
                context.ObjectContext.Add(graph, objID);

                XElement graphElement = SerializeObjectToNode(root, graph, context);

                root.SetAttributeValue("v", objID);
                root.SetAttributeValue("r", true);

                context.SerializeTypeInfo(root);

                return root;
            }
            finally
            {
                XElementFormatter.FormattingStatus = XElementFormattingStatus.None;
            }
        }
Esempio n. 16
0
        private void SerializableBinaryToNode(XElement parent, XmlObjectMappingItem item, object graph, XmlSerializeContext context)
        {
            //此处对没有标记为序列化的需要做一个异常处理,还是?
            XElement xElement = context.RootElement.AddChildElement("object");

            xElement.SetAttributeValue("id", context.CurrentID++);

            if (this.OutputShortType)
            {
                xElement.SetAttributeValue("shortType", graph.GetType().Name);
            }

            xElement.SetAttributeValue("oti", context.GetTypeID(graph.GetType()));

            XElement itemNode = xElement.AddChildElement("value");

            byte[] buffer = null;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, graph);
                buffer = ms.GetBuffer();
            }
            string imageBase64String = Convert.ToBase64String(buffer);

            itemNode.Add(imageBase64String);
        }
Esempio n. 17
0
        public void Serialize(XElement node, XmlSerializeContext context)
        {
            PropertyValue pv = this.Clone();
            int objID = 0;
            if (pv.Definition.DefaultValue.IsNullOrEmpty())
                pv.Definition.DefaultValue = string.Empty;

            if (pv.StringValue.IsNullOrEmpty())
                pv.StringValue = string.Empty;

            if (context.ObjectContext.TryGetValue(pv, out objID) == false)
            {
                objID = context.CurrentID;
                context.ObjectContext.Add(pv, objID);
                node.SetAttributeValue("id", context.CurrentID++);
                pv.Definition.Serialize(node, context);
            }
            else
            {
                node.SetAttributeValue("v", objID);
            }

            if (!pv.StringValue.IsNullOrEmpty() && pv.Definition.DefaultValue.ToLower() != pv.StringValue.ToLower())
                node.SetAttributeValue("_sv", pv.StringValue);

        }