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