Beispiel #1
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);
        }