-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathExportManager.cs
More file actions
71 lines (63 loc) · 2.39 KB
/
ExportManager.cs
File metadata and controls
71 lines (63 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.IO;
using System.Xml.Linq;
namespace NETworkManager.Models.Export;
/// <summary>
/// This class will provide methods to export text or objects to files like csv, xml, json or plain text.
/// </summary>
public static partial class ExportManager
{
/// <summary>
/// Default declaration for XML documents.
/// </summary>
private static readonly XDeclaration DefaultXDeclaration = new("1.0", "utf-8", "yes");
/// <summary>
/// Method to export text to a file.
/// </summary>
/// <param name="filePath">Path to the export file.</param>
/// <param name="content">Text to export.</param>
public static void Export(string filePath, string content)
{
CreateTxt(content, filePath);
}
/// <summary>
/// Create a text file from the given content.
/// </summary>
/// <param name="content">Content of the text file.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateTxt(string content, string filePath)
{
File.WriteAllText(filePath, content);
}
/// <summary>
/// Get the file extension as string from the given <see cref="ExportFileType" />.
/// </summary>
/// <param name="fileExtension">File extension as <see cref="ExportFileType" />.</param>
/// <returns>File extension as string.</returns>
public static string GetFileExtensionAsString(ExportFileType fileExtension)
{
return fileExtension switch
{
ExportFileType.Csv => "CSV",
ExportFileType.Xml => "XML",
ExportFileType.Json => "JSON",
ExportFileType.Txt => "TXT",
_ => string.Empty
};
}
/// <summary>
/// Escapes a string value for CSV format by wrapping it in quotes if it contains commas, quotes, or newlines.
/// </summary>
/// <param name="value">The string value to escape.</param>
/// <returns>The escaped CSV value.</returns>
internal static string EscapeCsvValue(string value)
{
if (string.IsNullOrEmpty(value))
return value;
// If the value contains comma, quote, or newline, wrap it in quotes and escape internal quotes
if (value.Contains(',') || value.Contains('"') || value.Contains('\n') || value.Contains('\r'))
{
return $"\"{value.Replace("\"", "\"\"")}\"";
}
return value;
}
}