Exemplo n.º 1
0
        private object GetObjectFromFormatter(XElement element)
        {
            var formatterAttrib = element.Attribute(XmlConstants.FORMATTER);

            if (formatterAttrib == null)
            {
                return(null);
            }

            var valueAttrib = element.Attribute(XmlConstants.VALUE);

            if (valueAttrib == null)
            {
                throw new XmlException("Missing value attribute for formattable element " + GetFullPath(element));
            }

            var formatterType = _typeCache[formatterAttrib.Value];
            SerializationFormatter formatter = (SerializationFormatter)formatterType.GetConstructor(Type.EmptyTypes).Invoke(Type.EmptyTypes);

            return(formatter.FromString(XmlHelper.UnEscapeInvalidCharacters(valueAttrib.Value)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates and populates an object from the given XML element.
        /// </summary>
        /// <param name="element">The element containing the object description</param>
        /// <param name="parent">The parent.</param>
        /// <returns>
        /// A populated object specified by the given XML element.
        /// </returns>
        private object ReadObject(XElement element, object parent)
        {
            // See if this element is an object reference
            var refElement = element.Attribute(XmlConstants.REF);

            if (refElement != null)
            {
                return(_references[refElement.Value]);
            }

            object result = GetObjectFromFormatter(element);

            if (result != null)
            {
                return(result);
            }

            Type   type  = GetType(element);
            string value = GetValue(element);

            if (type == null)
            {
                // must be a plugin
                throw new ArgumentNullException("Couldn't find the assembly that contains the type that was serialized into that element");
            }
            else if (type.IsPrimitive)
            {
                result = value != "NaN" ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) : Convert.ChangeType(Double.NaN, type);
            }
            else if (type.IsEnum)
            {
                result = Enum.Parse(type, value);
            }
            else if (type.Equals(typeof(string)))
            {
                result = XmlHelper.UnEscapeInvalidCharacters(value);
            }
            else if (type == typeof(DateTime))
            {
                result = Convert.ToDateTime(value);
            }
            else if (type.Equals(typeof(Color)))
            {
                result = ColorTranslator.FromHtml(value);
            }
            else if (type.Equals(typeof(PointF)))
            {
                string[] vals = XmlHelper.UnEscapeInvalidCharacters(value).Split('|');
                result = new PointF(float.Parse(vals[0]), float.Parse(vals[1]));
            }
            else
            {
                if (parent == null)
                {
                    result = ConstructObject(type, element);
                }
                else
                {
                    result = parent;
                }

                FillObject(element, result, false);
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates and populates an object from the given XML element.
        /// </summary>
        /// <param name="element">The element containing the object description</param>
        /// <param name="parent">The parent.</param>
        /// <returns>
        /// A populated object specified by the given XML element.
        /// </returns>
        private object ReadObject(XElement element, object parent)
        {
            // See if this element is an object reference
            var refElement = element.Attribute(XmlConstants.Ref);

            if (refElement != null)
            {
                return(_references[refElement.Value]);
            }

            object result = GetObjectFromFormatter(element);

            if (result != null)
            {
                return(result);
            }

            Type   type  = GetType(element);
            string value = GetValue(element);

            if (type == null)
            {
                // must be a plugin
                throw new FileNotFoundException("Couldn't find the assembly that contains the type that was serialized into that element.");
            }

            if (type.IsPrimitive)
            {
                result = value != "NaN" ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) : Convert.ChangeType(double.NaN, type);
            }
            else if (type.IsEnum)
            {
                result = Enum.Parse(type, value);
            }
            else if (type == typeof(string))
            {
                result = XmlHelper.UnEscapeInvalidCharacters(value);
            }
            else if (type == typeof(DateTime))
            {
                result = Convert.ToDateTime(value, CultureInfo.InvariantCulture);
            }
            else if (type == typeof(Color))
            {
                result = ColorTranslator.FromHtml(value);
            }
            else if (type == typeof(PointF))
            {
                string[] vals = XmlHelper.UnEscapeInvalidCharacters(value).Split('|');
                result = new PointF(float.Parse(vals[0]), float.Parse(vals[1]));
            }
            else
            {
                if (parent == null)
                {
                    try
                    {
                        result = ConstructObject(type, element);
                    }
                    catch
                    {
                        // If a project file (such as a layer) is missing, this exception is thrown.
                        // We still want to be able to open the project; setting result to null seems to make this work.
                        result = null;
                    }
                }
                else
                {
                    result = parent;
                }

                if (result != null)
                {
                    FillObject(element, result, false);
                }
            }

            return(result);
        }