コード例 #1
0
        private void ReadProperty(XmlReader reader, object target, XmlPropertyInfo[] propertyInfos, XmlMappingType mappingType, XmlSerializationContext context)
        {
            for (var i = 0; i < propertyInfos.Length; i++)
            {
                var member = propertyInfos[i].Match(mappingType, reader);

                if (member != null)
                {
                    if (propertyInfos[i].CollectionProxy == null)
                    {
                        var value = context.Deserialize(reader, member);
                        SetValue(target, propertyInfos[i].Property, value);
                    }
                    else
                    {
                        var value = context.Deserialize(reader, propertyInfos[i].Property.Item);
                        propertyInfos[i].CollectionProxy.Add(value);
                    }

                    propertyInfos[i].Assigned = true;
                    return;
                }
            }

            OnUnknownProperty(reader, target, context);
        }
コード例 #2
0
        public object ReadXml(XmlReader reader, XmlSerializationContext context)
        {
            if (context.Member.MappingType != XmlMappingType.Element)
            {
                throw new XmlSerializationException($"XML mapping of \"{context.ValueType}\" must be Element.");
            }

            var contract = context.Contract.ToObjectContract();
            var target   = CreateTarget(contract);

            var propertyInfos = XmlPropertyInfo.GetInfo(contract, reader.NameTable, context);

            if (reader.MoveToFirstAttribute())
            {
                do
                {
                    ReadProperty(reader, target, propertyInfos, XmlMappingType.Attribute, context);
                }while (reader.MoveToNextAttribute());

                reader.MoveToElement();
            }

            if (!reader.IsEmptyElement)
            {
                if (contract.InnerTextProperty != null)
                {
                    var value = context.Deserialize(reader, contract.InnerTextProperty);
                    SetValue(target, contract.InnerTextProperty, value);
                }
                else
                {
                    reader.ReadStartElement();

                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            ReadProperty(reader, target, propertyInfos, XmlMappingType.Element, context);
                        }
                        else
                        {
                            reader.Read();
                        }
                    }

                    reader.Read();
                }
            }
            else
            {
                reader.Read();
            }

            SetDefaultProperties(contract, target, propertyInfos);

            return(GetResult(target));
        }
コード例 #3
0
        /// <summary>
        /// Deserializes from string.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="type">The type.</param>
        /// <returns>System.Object.</returns>
        /// <inheritdoc />
        public object DeserializeFromString(string content, Type type)
        {
            CanDeserialize(type, true);
            var context = new XmlSerializationContext(Settings);

            using (var stream = new StringReader(content))
            {
                using (var reader = new XmlTextReader(stream))
                {
                    return(context.Deserialize(reader, type));
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Deserializes from stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="type">The type.</param>
        /// <returns>System.Object.</returns>
        /// <inheritdoc />
        public object DeserializeFromStream(Stream stream, Type type)
        {
            CanDeserialize(type, true);
            var context = new XmlSerializationContext(Settings);

            if (stream.Position == stream.Length)
            {
                stream.ResetPosition();
            }
            using (var reader = new XmlTextReader(stream))
            {
                return(context.Deserialize(reader, type));
            }
        }
コード例 #5
0
        public object ReadXml(XmlReader reader, XmlSerializationContext context)
        {
            var member         = context.Member;
            var underlyingType = member.ValueType.GetUnderlyingOptionalType();

            if (member.MappingType == XmlMappingType.Element)
            {
                if (!context.ReadValueType(reader, ref underlyingType))
                {
                    reader.Skip();
                    return(null);
                }
            }

            return(context.Deserialize(reader, underlyingType));
        }