Read() public method

public Read ( ) : bool
return bool
        public override bool Read()
        {
            if (_surrogateReader != null)
            {
                if (_surrogateReader.Read() && !IsAtDummyNodeEndElement())
                {
                    // If this isn't the closing dummy node, return true.
                    return(true);
                }

                // When we're done with surrogateReader, get rid of it and go back to primaryReader.
                _surrogateReader.Close();
                _surrogateReader = null;
                _currentReader   = _primaryReader;
            }

            var read = _primaryReader.Read();

            if (!read)
            {
                return(false);
            }

            // If we're decrypting, and our node contains text that starts with '<', then
            // assume that the text contains one or more encrypted child elements. In this
            // case, load the decrypted contents into a surrogate xml reader and use that
            // reader until it reads to its end. Then switch back to the primary reader.
            if (IsDecryptionEnabled && NodeType == XmlNodeType.Text)
            {
                var value = Value;

                if (_isStartElementRegex.IsMatch(value))
                {
                    // The xml fragment contained in the Value property may contain multiple
                    // top-level elements. To ensure valid xml, wrap the fragment in a dummy node.
                    var xml = _dummyNodeStartElement + value + _dummyNodeEndElement;

                    _surrogateReader = new XSerializerXmlReader(xml, _encryptionMechanism, _encryptKey,
                                                                _serializationState);
                    _currentReader = _surrogateReader;

                    _surrogateReader.Read();         // Advance to the opening dummy node
                    return(_surrogateReader.Read()); // Advance to the first decrypted node.
                }
            }

            return(true);
        }
        public override bool Read()
        {
            if (_surrogateReader != null)
            {
                if (_surrogateReader.Read() && !IsAtDummyNodeEndElement())
                {
                    // If this isn't the closing dummy node, return true.
                    return true;
                }

                // When we're done with surrogateReader, get rid of it and go back to primaryReader.
                _surrogateReader.Close();
                _surrogateReader = null;
                _currentReader = _primaryReader;
            }

            var read = _primaryReader.Read();

            if (!read)
            {
                return false;
            }

            // If we're decrypting, and our node contains text that starts with '<', then
            // assume that the text contains one or more encrypted child elements. In this
            // case, load the decrypted contents into a surrogate xml reader and use that
            // reader until it reads to its end. Then switch back to the primary reader.
            if (IsDecryptionEnabled && NodeType == XmlNodeType.Text)
            {
                var value = Value;

                if (_isStartElementRegex.IsMatch(value))
                {
                    // The xml fragment contained in the Value property may contain multiple
                    // top-level elements. To ensure valid xml, wrap the fragment in a dummy node.
                    var xml = _dummyNodeStartElement + value + _dummyNodeEndElement;

                    _surrogateReader = new XSerializerXmlReader(xml, _encryptionMechanism, _encryptKey,
                        _serializationState);
                    _currentReader = _surrogateReader;

                    _surrogateReader.Read(); // Advance to the opening dummy node
                    return _surrogateReader.Read(); // Advance to the first decrypted node.
                }
            }

            return true;
        }
示例#3
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            if (ValueTypes.IsRegistered(typeof(T)))
            {
                while (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Read();
                }
            }

            if (reader.IsNil())
            {
                return(default(T));
            }

            var setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

            var value = reader.ReadString();

            if (setIsDecryptionEnabledBackToFalse)
            {
                reader.IsDecryptionEnabled = false;
            }

            return(_valueConverter.ParseString(value, options));
        }
示例#4
0
        internal static bool ReadIfNeeded(this XSerializerXmlReader reader, bool shouldRead)
        {
            if (shouldRead)
            {
                return(reader.Read());
            }

            return(true);
        }
示例#5
0
        private dynamic DeserializeToDynamic(XSerializerXmlReader reader, ISerializeOptions options)
        {
            object instance = null;
            var    hasInstanceBeenCreated = false;

            var setIsDecryptionEnabledBackToFalse = false;

            Func <bool> isAtRootElement;

            {
                var hasOpenedRootElement = false;

                isAtRootElement = () =>
                {
                    if (!hasOpenedRootElement && reader.Name == _options.RootElementName)
                    {
                        hasOpenedRootElement = true;
                        return(true);
                    }

                    return(false);
                };
            }

            do
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (isAtRootElement())
                    {
                        setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                        instance = new ExpandoObject();
                        hasInstanceBeenCreated = true;

                        if (reader.IsEmptyElement)
                        {
                            if (_options.TreatEmptyElementAsString)
                            {
                                instance = "";
                            }

                            if (setIsDecryptionEnabledBackToFalse)
                            {
                                reader.IsDecryptionEnabled = false;
                            }

                            return(instance);
                        }
                    }
                    else
                    {
                        SetElementPropertyValue(reader, hasInstanceBeenCreated, options, (ExpandoObject)instance);
                    }
                    break;

                case XmlNodeType.Text:
                    var stringValue = (string)new XmlTextSerializer(typeof(string), _options.RedactAttribute, null, _options.ExtraTypes).DeserializeObject(reader, options);
                    hasInstanceBeenCreated = true;

                    bool boolValue;
                    if (bool.TryParse(stringValue, out boolValue))
                    {
                        instance = boolValue;
                        break;
                    }

                    int intValue;
                    if (int.TryParse(stringValue, out intValue))
                    {
                        // If this is a number with leading zeros, treat it as a string so we don't lose those leading zeros.
                        if (stringValue[0] == '0' && stringValue.Length > 1)
                        {
                            instance = stringValue;
                        }
                        else
                        {
                            instance = intValue;
                        }

                        break;
                    }

                    decimal decimalValue;
                    if (decimal.TryParse(stringValue, out decimalValue))
                    {
                        instance = decimalValue;
                        break;
                    }

                    DateTime dateTimeValue;
                    if (DateTime.TryParse(stringValue, out dateTimeValue))
                    {
                        instance = dateTimeValue.ToUniversalTime();
                        break;
                    }

                    // TODO: add more types to check?

                    instance = stringValue;
                    break;

                case XmlNodeType.EndElement:
                    if (reader.Name == _options.RootElementName)
                    {
                        if (_options.TreatEmptyElementAsString)
                        {
                            var instanceAsExpando = instance as IDictionary <string, object>;
                            if (instanceAsExpando != null && instanceAsExpando.Count == 0)
                            {
                                instance = "";
                            }
                        }

                        if (setIsDecryptionEnabledBackToFalse)
                        {
                            reader.IsDecryptionEnabled = false;
                        }

                        return(CheckAndReturn(hasInstanceBeenCreated, instance));
                    }
                    break;
                }
            } while (reader.Read());

            throw new InvalidOperationException("Deserialization error: reached the end of the document without returning a value.");
        }