Example #1
0
        public void Serialize(object obj, String name, XmlNode parent)
        {
            // Reset();

            XmlDocument doc = parent.OwnerDocument;

            XmlElement root = doc.CreateElement(_taglib.OBJECT_TAG);

            parent.AppendChild(root);

            XmlComment comment = root.OwnerDocument.CreateComment(" Data section : Don't edit any attributes ! ");

            root.AppendChild(comment);

            SetObjectInfoAttributes(name, obj.GetType(), root);

            Type ctortype = TypeInfo.GetBinaryConstructorType(obj.GetType());

            if (ctortype != null)
            {
                SerializeBinaryObject(obj, ctortype, root);
            }
            else
            {
                SerializeProperties(obj, root);
            }

            WriteTypeDictionary(root);
        }
Example #2
0
        protected void SetProperty(object obj, object value, PropertyInfo pi, XmlNode parent)
        {
            //      object val = value; ??

            try
            {
                // Empty values are ignored (no need to restore null references or empty Strings)
                if (value == null || value.Equals(""))
                {
                    return;
                }

                // Get the Type
                //Type pt = pi.PropertyType;
                Type pt = value.GetType();

                // Check whether this property can be serialized and deserialized
                if (CheckPropertyHasToBeSerialized(pi) && (pt.IsSerializable || IgnoreSerializableAttribute) &&
                    (pi.CanWrite) && ((pt.IsPublic) || (pt.IsEnum)))
                {
                    XmlElement prop = parent.OwnerDocument.CreateElement(_taglib.PROPERTY_TAG);

                    SetObjectInfoAttributes(pi.Name, pt, prop);

                    // Try to find a constructor for binary data.
                    // If found remember the parameter's Type.
                    Type binctortype = TypeInfo.GetBinaryConstructorType(pt);

                    if (binctortype != null) // a binary contructor was found
                    {
                        /*
                         * b. Trying to handle binary data
                         */

                        SerializeBinaryObject(pi.GetValue(obj, null), binctortype, prop);
                    }
                    else if (TypeInfo.IsCollection(pt))
                    {
                        /*
                         * a. Collections ask for a specific handling
                         */
                        SetCollectionItems(obj, (ICollection)value, prop);
                    }
                    else
                    {
                        /*
                         * c. "normal" classes
                         */

                        SetXmlElementFromBasicPropertyValue(prop, pt, value, parent);
                    }
                    // Append the property node to the paren XmlNode
                    parent.AppendChild(prop);
                }
            }
            catch (Exception exc)
            {
                if (!IgnoreSerialisationErrors)
                {
                    throw exc;
                }
                else
                {
                    // perhaps logging
                }
            }
        }