コード例 #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>
        /// Initializes a new instance of the <see cref="FileCabinetRecordXmlWriter"/> class.
        /// </summary>
        /// <param name="writer">The xml writer.</param>
        /// <param name="records">The serializable records.</param>
        public FileCabinetRecordXmlWriter(XmlWriter writer, SerializableCollection records)
        {
            if (writer is null)
            {
                throw new ArgumentNullException($"{nameof(writer)} cannot be null.");
            }

            if (records is null)
            {
                throw new ArgumentNullException($"{nameof(records)} cannot be null.");
            }

            this.writer     = writer;
            this.serializer = new XmlSerializer(typeof(SerializableCollection));
            this.records    = records;
        }