public void Read(KeePassPackage package,  Stream stream)
        {
            var settings = new XmlReaderSettings()
            {
                CloseInput = true,
                IgnoreComments = true,
                IgnoreProcessingInstructions = true,
                IgnoreWhitespace = true,

                DtdProcessing = DtdProcessing.Prohibit
            };

#if XML_FULL
            settings.ValidationType = ValidationType.None;
#endif 

            XmlReader reader = XmlReader.Create(stream, settings);

            reader.Read();

            var byteGeneratorId = package.HeaderInfo.RandomByteGeneratorCryptoType;
            var byteGenerator = RandomByteGeneratorFactory.GetGenerator(byteGeneratorId);
            byteGenerator.Initialize(package.HeaderInfo.RandomByteGeneratorCryptoKey);

            var serializerContext = new SerializerContext()
            {
                RandomByteGenerator = byteGenerator,
                Binaries = package.Binaries,
                DatabaseCompression = package.HeaderInfo.DatabaseCompression,
            };

            var metaSerializer = new MetaXmlSerializer(package.Meta, serializerContext);
            metaSerializer.ReadXml(reader);

            // set memory protection options. 
            serializerContext.MemoryProtection = package.Meta.MemoryProtection;

            var rootSerializer = new RootXmlSerializer(package.Root, serializerContext);
            rootSerializer.ReadXml(reader);

            package.Binaries = serializerContext.Binaries;
        }
        public void Write(KeePassPackage package,  Stream stream)
        {
            var settings = new XmlWriterSettings()
            {
                Indent = true,
                IndentChars = "\t",
                Encoding = new UTF8Encoding(false, false)
            };

#if XML_FULL
            settings.ValidationType = ValidationType.None;
#endif 

            using (XmlWriter writer = XmlWriter.Create(stream, settings))
            {
                writer.WriteStartElement("KeePassFile");

                var byteGeneratorId = package.HeaderInfo.RandomByteGeneratorCryptoType;
                var byteGenerator = RandomByteGeneratorFactory.GetGenerator(byteGeneratorId);
                byteGenerator.Initialize(package.HeaderInfo.RandomByteGeneratorCryptoKey);

                var serializerContext = new SerializerContext()
                {
                    RandomByteGenerator = byteGenerator,
                    MemoryProtection = package.Meta.MemoryProtection,
                    Binaries = package.Binaries
                };

                var metaSerializer = new MetaXmlSerializer(package.Meta, serializerContext);
                metaSerializer.WriteXml(writer);

                var rootSerializer = new RootXmlSerializer(package.Root, serializerContext);
                rootSerializer.WriteXml(writer);

                writer.WriteEndElement();
                writer.Flush();
            }
        }