/// <summary>
 /// Saves data to xml file.
 /// </summary>
 /// <param name="writer">Provider for writing.</param>
 /// <param name="snapshot">The state to be saved.</param>
 private static void ExportToXml(StreamWriter writer, FileCabinetServiceSnapshot snapshot)
 {
     snapshot.SaveToXml(writer);
 }
示例#2
0
        private void Export(string parameters)
        {
            string[] parametersArr = parameters.Split(' ', 2);
            if (parametersArr.Length < 2)
            {
                Console.WriteLine(Configurator.GetConstantString("InvalidInput"));
                Console.WriteLine(Configurator.GetConstantString("CommandPatthern"));
                Console.WriteLine(Configurator.GetConstantString("ExportPatthern"));
                return;
            }

            const int exportTypeIndex = 0;
            const int filePathIndex   = 1;

            if (File.Exists(parametersArr[filePathIndex]))
            {
                Console.WriteLine($"File is exist - rewrite {parametersArr[filePathIndex]}? [Y/n]");
                string answer = Console.ReadLine();
                if (answer.Equals(Configurator.GetConstantString("PositiveAnswer"), StringComparison.OrdinalIgnoreCase))
                {
                    File.Delete(parametersArr[filePathIndex]);
                }
                else
                {
                    return;
                }
            }

            FileCabinetServiceSnapshot snapshot = this.Service.MakeSnapshot();

            if (parametersArr[exportTypeIndex].Equals(Configurator.GetConstantString("CsvType"), StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    using StreamWriter streamWriter = new StreamWriter(parametersArr[filePathIndex]);
                    streamWriter.WriteLine(Configurator.GetConstantString("CsvHeader"));
                    snapshot.SaveToCsv(streamWriter);
                    Console.WriteLine($"All records are exported to file {parametersArr[filePathIndex]}");
                }
                catch (DirectoryNotFoundException)
                {
                    Console.WriteLine(Configurator.GetConstantString("DirectoryNotExist"));
                }
            }
            else if (parametersArr[exportTypeIndex].Equals(Configurator.GetConstantString("XmlType"), StringComparison.OrdinalIgnoreCase))
            {
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    Indent      = true,
                    IndentChars = "\t",
                };
                try
                {
                    using XmlWriter xmlWriter = XmlWriter.Create(parametersArr[filePathIndex], settings);
                    snapshot.SaveToXml(xmlWriter);
                    Console.WriteLine($"All records are exported to file {parametersArr[filePathIndex]}");
                }
                catch (DirectoryNotFoundException)
                {
                    Console.WriteLine(Configurator.GetConstantString("DirectoryNotExist"));
                }
            }
            else
            {
                Console.WriteLine(Configurator.GetConstantString("InvalidFormatType"));
            }
        }
示例#3
0
        private void Export(string parameters)
        {
            bool         rewrite   = false;
            var          noRewrite = 'n';
            const string xml       = "xml";
            const string csv       = "csv";

            try
            {
                snapshot = this.service.MakeSnapshot();
                var parameterArray = parameters.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                if (parameterArray.Length != 2)
                {
                    throw new ArgumentException("You did not specify the type of file to export or the path to export");
                }

                var fullPath      = parameterArray.Last();
                var nameFile      = Path.GetFileName(fullPath);
                var typeFile      = parameterArray.First();
                var extensionFile = Path.GetExtension(nameFile).TrimStart('.');
                if (typeFile != extensionFile)
                {
                    throw new ArgumentException($"You want to export data to a {nameFile}, but you specified the type {typeFile}");
                }

                if (File.Exists(fullPath))
                {
                    Console.Write($"File is exist - rewrite {nameFile}?[Y / n] ");
                    var rewriteOrNo = ReadInput(RewriteConverter, RewriteValidator);
                    char.ToLower(rewriteOrNo, CultureInfo.InvariantCulture);
                    if (char.Equals(rewriteOrNo, noRewrite))
                    {
                        rewrite = true;
                    }
                }

                try
                {
                    if (string.Equals(csv, typeFile, StringComparison.OrdinalIgnoreCase))
                    {
                        using (var sw = new StreamWriter(fullPath, rewrite))
                        {
                            snapshot.SaveToCsv(sw);
                            Console.WriteLine($"All records are exported to file {nameFile}");
                        }
                    }
                    else if (string.Equals(xml, typeFile, StringComparison.OrdinalIgnoreCase))
                    {
                        using (var sw = new StreamWriter(fullPath, rewrite))
                        {
                            snapshot.SaveToXml(sw);
                            Console.WriteLine($"All records are exported to file {nameFile}");
                        }
                    }
                }
                catch (DirectoryNotFoundException)
                {
                    Console.WriteLine($"Export failed: can't open file {fullPath}");
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Enter the file extension and his name or path");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }