コード例 #1
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 item = GetCollectionItem(context);

            if (item == null)
            {
                throw new XmlSerializationException(string.Format("XML contract of \"{0}\" must contains item info", context.ValueType));
            }

            var collectionProxy = CreateProxy(context.ValueType);

            if (!reader.IsEmptyElement)
            {
                Type           lastItemType = null;
                XmlTypeContext typeContext  = null;
                var            itemInfo     = new XmlItemInfo(item, reader.NameTable);

                reader.ReadStartElement();

                var nodeType = reader.NodeType;

                while (nodeType != XmlNodeType.EndElement)
                {
                    if (nodeType == XmlNodeType.Element)
                    {
                        var member = itemInfo.Match(reader);

                        if (member != null)
                        {
                            var valueType = member.ValueType;

                            if (context.ReadValueType(reader, ref valueType))
                            {
                                if (lastItemType != valueType)
                                {
                                    typeContext  = context.Settings.GetTypeContext(valueType);
                                    lastItemType = valueType;
                                }

                                var value = context.ReadXml(reader, member, typeContext);
                                collectionProxy.Add(value);
                            }
                            else
                            {
                                reader.Skip();
                            }
                        }
                        else
                        {
                            reader.Skip();
                        }
                    }
                    else
                    {
                        reader.Read();
                    }

                    nodeType = reader.NodeType;
                }
            }

            reader.Read();

            return(collectionProxy.GetResult());
        }