/// <summary> /// Loads records from xml file. /// </summary> /// <param name="stream">Current stream.</param> /// <returns>Count loaded records.</returns> internal int LoadFromXml(StreamReader stream) { FileCabinetRecordXmlReader reader = new FileCabinetRecordXmlReader(stream); this.records = reader.ReadAll().ToArray(); return(this.records.Length); }
/// <summary> /// Loads the records from xml file. /// </summary> /// <param name="reader">Xml reader.</param> /// <returns>List of imported records.</returns> public IList <FileCabinetRecord> LoadFromXml(StreamReader reader) { XmlReader xmlReader = XmlReader.Create(reader); FileCabinetRecordXmlReader fileXmlReader = new FileCabinetRecordXmlReader(xmlReader); IList <FileCabinetRecord> records = fileXmlReader.ReadAll(); return(records); }
/// <summary> /// Loads record from XML-file. /// </summary> /// <param name="reader">Stream to read.</param> /// <returns>Count of records.</returns> public int LoadFromXml(StreamReader reader) { var xmlReader = new FileCabinetRecordXmlReader(reader); this.records = xmlReader.ReadAll(); return(this.records.Count); }
/// <summary> /// Метод выгрузки из xml. /// </summary> /// <param name="fs">Экзмепляр Filestream.</param> public void LoadFromXml(FileStream fs) { StreamReader reader = new StreamReader(fs); FileCabinetRecordXmlReader xmlReader = new FileCabinetRecordXmlReader(reader); List <FileCabinetRecord> list = new List <FileCabinetRecord>(xmlReader.ReadAll()); this.records = list.ToArray(); }
/// <summary> /// Load data from xml file. /// </summary> /// <param name="filestream">Input filestream.</param> public void LoadFromXml(FileStream filestream) { if (filestream == null) { throw new ArgumentNullException(nameof(filestream)); } using (XmlReader xmlReader = XmlReader.Create(filestream.Name)) { this.fileCabinetRecordXmlReader = new FileCabinetRecordXmlReader(xmlReader); this.ListFromFile = new ReadOnlyCollection<FileCabinetRecord>(this.fileCabinetRecordXmlReader.ReadAll()); } }
/// <summary> /// Gets records from xml. /// </summary> /// <param name="xmlReader">Xml reader to get records.</param> public void LoadFromXml(XmlReader xmlReader) { FileCabinetRecordXmlReader fileXmlReader = new FileCabinetRecordXmlReader(xmlReader); try { this.records = fileXmlReader.ReadAll().ToArray(); } catch (InvalidOperationException) { Console.WriteLine(Configurator.GetConstantString("InvalidDeserialize")); } }
private void ImportFromFormat(string format, string path) { string message = string.Empty; if (!File.Exists(path)) { message = $"Import error: file {path} does not exist."; } FileStream fileStream = default; try { fileStream = new FileStream(path, FileMode.Open); } catch (UnauthorizedAccessException ex) { Console.WriteLine(ex.Message); } catch (FileNotFoundException) { Console.WriteLine($"Export failed: can't open file {path}."); } catch (IOException ex) { Console.WriteLine(ex.Message); } if (fileStream != null) { using var stream = new StreamReader(fileStream); var snapshot = new FileCabinetServiceSnapshot(Array.Empty <FileCabinetRecord>()); if (format.Equals(CsvString, StringComparison.InvariantCultureIgnoreCase)) { using var reader = new FileCabinetRecordCsvReader(stream); message = this.LoadFrom(reader, path, snapshot); } if (format.Equals(XmlString, StringComparison.InvariantCultureIgnoreCase)) { using var reader = new FileCabinetRecordXmlReader(stream); message = this.LoadFrom(reader, path, snapshot); } } Console.WriteLine(message); }
/// <summary>Loads records from XML file.</summary> /// <param name="reader">Reader.</param> /// <param name="validator">Validator.</param> /// <exception cref="ArgumentNullException">Thrown when reader or validator is null.</exception> public void LoadFromXml(XmlReader reader, IRecordValidator validator) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (validator == null) { throw new ArgumentNullException(nameof(validator)); } var xmlReader = new FileCabinetRecordXmlReader(reader); IList <FileCabinetRecord> records; try { records = xmlReader.ReadAll(); } catch (InvalidOperationException) { throw; } UnverifiedData unverifiedData; foreach (var record in records) { unverifiedData = new UnverifiedData(record); try { validator.ValidateParameters(unverifiedData); } catch (ArgumentException ex) { this.invalidRecords.Add(new Tuple <FileCabinetRecord, string>(record, ex.Message)); continue; } this.records.Add(record); } }
/// <summary>Loads snapshot from XML file.</summary> /// <param name="fs">The stream for a file.</param> public void LoadFromXml(FileStream fs) { int overwrittenElements = 0; int lastElementIndex = this.records.Length - 1; IList <FileCabinetRecord> list = new FileCabinetRecordXmlReader(fs).ReadAll(); Array.Resize(ref this.records, this.records.Length + list.Count); foreach (var element in list) { if (lastElementIndex < 0) { this.records[lastElementIndex + 1] = element; lastElementIndex++; continue; } for (int i = 0; i <= lastElementIndex; i++) { if (this.records[i].Id == element.Id) { this.records[i] = element; overwrittenElements++; break; } if (i == lastElementIndex) { this.records[lastElementIndex + 1] = element; lastElementIndex++; break; } } } Array.Resize(ref this.records, this.records.Length - overwrittenElements); }
/// <summary> /// This Methods load from file in xml-format. /// </summary> /// <param name="streamReader">Represents characters read.</param> /// <returns>List records.</returns> public IList <FileCabinetRecord> LoadFromXml(StreamReader streamReader) { FileCabinetRecordXmlReader xmlReader = new FileCabinetRecordXmlReader(streamReader); return(xmlReader.ReadAll()); }
/// <summary> /// Load records from xml file. /// </summary> /// <param name="reader">Stream Reader.</param> public void LoadFromXml(StreamReader reader) { FileCabinetRecordXmlReader xmlReader = new FileCabinetRecordXmlReader(reader); this.records = xmlReader.ReadAll().ToArray(); }