Пример #1
0
 internal void BeginRead(XmlReaderDelegator xmlReader)
 {
     if (xmlReader.NodeType != XmlNodeType.Element)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializerReadContext.CreateUnexpectedStateException(XmlNodeType.Element, xmlReader));
     }
     _xmlReader          = xmlReader;
     _startDepth         = xmlReader.Depth;
     _innerReader        = xmlReader.UnderlyingReader;
     _isRootEmptyElement = InnerReader.IsEmptyElement;
 }
 void HandleUnexpectedItemInCollection(ref int iterator)
 {
     if (IsStartElement())
     {
         context.SkipUnknownElement(xmlReader);
         iterator--;
     }
     else
     {
         throw XmlObjectSerializerReadContext.CreateUnexpectedStateException(XmlNodeType.Element, xmlReader);
     }
 }
Пример #3
0
        private object ReadCollectionItems(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, CollectionDataContract collectionContract, object resultCollection, bool isReadOnlyCollection)
        {
            string itemName = GetCollectionContractItemName(collectionContract);
            string itemNs   = GetCollectionContractNamespace(collectionContract);
            Type   itemType = collectionContract.ItemType;
            CollectionReadItemDelegate collectionReadItemDelegate = GetCollectionReadItemDelegate(collectionContract);
            MethodInfo getCollectionSetItemDelegateMethod         = s_getCollectionSetItemDelegateMethod.MakeGenericMethod(itemType);
            CollectionSetItemDelegate collectionSetItemDelegate   = (CollectionSetItemDelegate)getCollectionSetItemDelegateMethod.Invoke(this, new object[] { collectionContract, resultCollection, isReadOnlyCollection });

            int index = 0;

            while (true)
            {
                if (xmlReader.IsStartElement(collectionItemName, collectionItemNamespace))
                {
                    object collectionItem = collectionReadItemDelegate(xmlReader, context, collectionContract, itemType, itemName, itemNs);
                    resultCollection = collectionSetItemDelegate(resultCollection, collectionItem, index);
                    index++;
                }
                else
                {
                    if (xmlReader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }

                    if (!xmlReader.IsStartElement())
                    {
                        throw XmlObjectSerializerReadContext.CreateUnexpectedStateException(XmlNodeType.Element, xmlReader);
                    }

                    context.SkipUnknownElement(xmlReader);
                }
            }

            context.IncrementItemCount(index);

            if (!isReadOnlyCollection && IsArrayLikeCollection(collectionContract))
            {
                MethodInfo trimArraySizeMethod = XmlFormatGeneratorStatics.TrimArraySizeMethod.MakeGenericMethod(itemType);
                resultCollection = trimArraySizeMethod.Invoke(null, new object[] { resultCollection, index });
            }

            return(resultCollection);
        }
Пример #4
0
 internal void EndRead()
 {
     if (_isRootEmptyElement)
     {
         _xmlReader.Read();
     }
     else
     {
         if (_xmlReader.IsStartElement() && _xmlReader.Depth == _startDepth)
         {
             _xmlReader.Read();
         }
         while (_xmlReader.Depth > _startDepth)
         {
             if (!_xmlReader.Read())
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializerReadContext.CreateUnexpectedStateException(XmlNodeType.EndElement, _xmlReader));
             }
         }
     }
 }