コード例 #1
0
        protected internal override List <T> Deserialize(IntermediateReader input, ContentSerializerAttribute format, List <T> existingInstance)
        {
            var result = existingInstance ?? new List <T>();

            if (_itemSerializer is ElementSerializer <T> elementSerializer)
            {
                elementSerializer.Deserialize(input, result);
            }
            else
            {
                // Create the item serializer attribute.
                var itemFormat = new ContentSerializerAttribute
                {
                    ElementName = format.CollectionItemName
                };

                // Read all the items.
                while (input.MoveToElement(itemFormat.ElementName))
                {
                    var value = input.ReadObject <T>(itemFormat, _itemSerializer);
                    result.Add(value);
                }
            }

            return(result);
        }
コード例 #2
0
 public void Deserialize(IntermediateReader input, object collection, ContentSerializerAttribute format)
 {
     var itemFormat = new ContentSerializerAttribute
     {
         ElementName = format.CollectionItemName
     };
     while (input.MoveToElement(format.CollectionItemName))
         _addMethod.Invoke(collection, new[] { input.ReadObject<object>(itemFormat, _contentSerializer) });
 }
コード例 #3
0
        protected internal override Dictionary <TKey, TValue> Deserialize(IntermediateReader input, ContentSerializerAttribute format, Dictionary <TKey, TValue> existingInstance)
        {
            var result = existingInstance ?? new Dictionary <TKey, TValue>();

            while (input.MoveToElement(format.CollectionItemName))
            {
                input.Xml.ReadStartElement();

                var key   = input.ReadObject <TKey>(_keyFormat, _keySerializer);
                var value = input.ReadObject <TValue>(_valueFormat, _valueSerializer);
                result.Add(key, value);

                input.Xml.ReadEndElement();
            }

            return(result);
        }
コード例 #4
0
        public static T Deserialize <T>(XmlReader input, string referenceRelocationPath)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            var serializer = new IntermediateSerializer();
            var reader     = new IntermediateReader(serializer, input, referenceRelocationPath);
            T   asset;

            try
            {
                if (!reader.MoveToElement("XnaContent"))
                {
                    throw new InvalidContentException(
                              $"Could not find XnaContent element in '{referenceRelocationPath}'.");
                }

                // Initialize the namespace lookups from
                // the attributes on the XnaContent element.
                serializer.CreateNamespaceLookup(input);

                // Move past the XnaContent.
                input.ReadStartElement();

                // Read the asset.
                var format = new ContentSerializerAttribute {
                    ElementName = "Asset"
                };
                asset = reader.ReadObject <T>(format);

                // Process the shared resources and external references.
                reader.ReadSharedResources();
                reader.ReadExternalReferences();

                // Move past the closing XnaContent element.
                input.ReadEndElement();
            }
            catch (XmlException xmlException)
            {
                throw reader.Exception_InvalidContent(xmlException, "An error occured parsing.");
            }
            return(asset);
        }
コード例 #5
0
        protected internal override object Deserialize(
            IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = (IList)(existingInstance ?? Activator.CreateInstance(TargetType));

            // Create the item serializer attribute.
            var itemFormat = new ContentSerializerAttribute
            {
                ElementName = format.CollectionItemName
            };

            // Read all the items.
            while (input.MoveToElement(itemFormat.ElementName))
            {
                var value = input.ReadObject <object>(itemFormat);
                result.Add(value);
            }

            return(result);
        }
コード例 #6
0
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = existingInstance;

            if (result == null)
            {
                try
                {
                    result = Activator.CreateInstance(TargetType, true);
                }
                catch (MissingMethodException e)
                {
                    throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e);
                }
            }

            // First deserialize the base type.
            if (_baseSerializer != null)
            {
                _baseSerializer.Deserialize(input, format, result);
            }

            // Now deserialize our own elements.
            foreach (var info in _elements)
            {
                if (!info.Attribute.FlattenContent)
                {
                    if (!input.MoveToElement(info.Attribute.ElementName))
                    {
                        // If the the element was optional then we can
                        // safely skip it and continue.
                        if (info.Attribute.Optional)
                        {
                            continue;
                        }

                        // We failed to find a required element.
                        throw input.Exception_InvalidContent(null, "The Xml element `{0}` is required, but element `{1}` was found at line {2}:{3}. Try changing the element order or adding missing elements.", info.Attribute.ElementName, input.Xml.Name, ((IXmlLineInfo)input.Xml).LineNumber, ((IXmlLineInfo)input.Xml).LinePosition);
                    }
                }

                if (info.Attribute.SharedResource)
                {
                    input.ReadSharedResource(info.Attribute, (object o) => info.Setter(result, o));
                }
                else if (info.Setter == null)
                {
                    var value = info.Getter(result);
                    input.ReadObject(info.Attribute, info.Serializer, value);
                }
                else
                {
                    var value = input.ReadObject <object>(info.Attribute, info.Serializer);
                    info.Setter(result, value);
                }
            }

            if (_collectionHelper != null)
            {
                _collectionHelper.Deserialize(input, result, format);
            }

            return(result);
        }