コード例 #1
0
        public static           XmlPropertyInfo[] GetInfo(XmlObjectContract contract, XmlNameTable nameTable, XmlSerializationContext context)
        {
            var propertyInfos = new XmlPropertyInfo[contract.Properties.Count];

            for (var i = 0; i < propertyInfos.Length; i++)
            {
                var property = contract.Properties[i];
                propertyInfos[i].Property = property;

                if (!property.IsCollection)
                {
                    propertyInfos[i].NameRef.Reset(property.Name, nameTable);
                    propertyInfos[i].KnownNameRefs = XmlItemInfo.GetKnownNameRefs(property, nameTable);
                }
                else
                {
                    var typeContext         = context.Settings.GetTypeContext(property.ValueType);
                    var collectionConverter = typeContext.ReadConverter as XmlCollectionConverter;

                    if (collectionConverter == null)
                    {
                        throw new XmlSerializationException(string.Format("Readable collection converter for the type \"{0}\" is not found.", property.ValueType));
                    }

                    var item = property.Item ?? typeContext.Contract.Root;

                    propertyInfos[i].CollectionProxy = collectionConverter.CreateProxy(property.ValueType);
                    propertyInfos[i].Item            = item;
                    propertyInfos[i].NameRef.Reset(item.Name, nameTable);
                    propertyInfos[i].KnownNameRefs = XmlItemInfo.GetKnownNameRefs(item, nameTable);
                }
            }

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

            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));
        }