-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
89 lines (79 loc) · 3.1 KB
/
Program.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Xml.Serialization;
using System.Text.Json;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace CSVXMLJSON
{
public class Entry
{
public string Category { get; set; }
public bool ForAll { get; set; }
public string ParentCategory { get; set; }
public List<string> Values { get; set; }
}
public class Category
{
public string Name { get; set; }
[XmlArrayItem("Category", IsNullable = false)]
public Category[] Children { get; set; }
[XmlAttribute("everyone")]
public bool Everyone { get; set; }
}
class Program
{
static void Main(string[] args)
{
var ser = new XmlSerializer(typeof(Category));
Category rootCategory;
using (var s = File.OpenText(@"XMLTZ.xml"))
rootCategory = (Category)ser.Deserialize(s);
Dictionary<string, Entry> entries = new();
FillDictionary(rootCategory, null);
void FillDictionary(Category category, string parentName)
{
var entry = new Entry()
{
ParentCategory = parentName,
Category = category.Name,
ForAll = category.Everyone
};
var fullName = parentName == null ? category.Name : $"{parentName}.{category.Name}";
entries[fullName] = entry;
if (category.Children != null)
foreach (var childCategory in category.Children)
FillDictionary(childCategory, fullName);
}
using (var csv = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"CSVTZ.csv"))
{
csv.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
csv.SetDelimiters(";");
while (!csv.EndOfData)
{
string[] fields = csv.ReadFields();
var categoryName = fields[0];
var modelName = fields[1];
if (!entries.TryGetValue(categoryName, out var entry))
throw new Exception($"Unexpected category: {categoryName}");
entry.Values ??= new();
entry.Values.Add(modelName);
}
}
var json = JsonSerializer.Serialize(
entries.Values.Where(v => v.Values != null),
new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
Console.WriteLine(json);
using (var stream = File.Create(@"JSONTZ.json"))
using (var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }))
JsonSerializer.Serialize(
writer,
entries.Values.Where(v => v.Values != null),
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
}
}
}