Example #1
0
        /// <summary>
        /// Save records array to xml file.
        /// </summary>
        /// <param name="writer">The xml writer.</param>
        public void SaveToXML(StreamWriter writer)
        {
            if (writer is null)
            {
                throw new ArgumentNullException($"{nameof(writer)} cannot be null.");
            }

            var collection = new List <SerializableRecord>();

            foreach (var record in this.records)
            {
                var serializeRecord = new SerializableRecord();
                serializeRecord.Id          = record.Id;
                serializeRecord.FirstName   = record.FirstName;
                serializeRecord.LastName    = record.LastName;
                serializeRecord.dateOfBirth = record.DateOfBirth;
                serializeRecord.Experience  = record.Experience;
                serializeRecord.Account     = record.Account;
                serializeRecord.Gender      = record.Gender;

                collection.Add(serializeRecord);
            }

            var serializableRecords = new SerializableCollection();

            serializableRecords.SerializeRecords = collection.ToArray();

            var xmlWriter = new FileCabinetRecordXmlWriter(XmlWriter.Create(writer), serializableRecords);

            xmlWriter.Write();
        }
        /// <summary>
        /// Saves to XML.
        /// </summary>
        /// <param name="writer">The writer.</param>
        public void SaveToXml(StreamWriter writer)
        {
            FileCabinetRecordXmlWriter writeToFile = new FileCabinetRecordXmlWriter(writer);

            writeToFile.WriteHeader();
            foreach (var record in this.records)
            {
                writeToFile.Write(record);
            }

            writeToFile.WriteFooter();
        }
        /// <summary>
        /// Saves to XML.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <exception cref="ArgumentNullException">Throws when stream is null.</exception>
        public void SaveToXml(StreamWriter stream)
        {
            if (stream is null)
            {
                throw new ArgumentNullException($"{nameof(stream)} must nit be null");
            }

            FileCabinetRecordXmlWriter writer = new FileCabinetRecordXmlWriter(stream);
            var doc = new XmlDocument();

            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
            doc.AppendChild(doc.CreateElement("ArrayOfFileCabinetRecord"));
            doc.Save(stream);
            foreach (var item in this.records)
            {
                writer.Write(item);
            }
        }