when running the code, a terminal is launched in which all the text from my file is shown, but then when opening the file it is not there. What could be wrong. my code is so that each attribute can find its line by the letter from the first column and determine what information it needs, for example, when it sees a RE, it puts information from the second column along this line in attribute1, and attribute2 from the third column. when he sees the letter T, attribute 3 takes from the second column, attribute 4 from the third column and AdditionalElements from the sixth. When he sees the letter H, attribute5 uses information from 2 columns, attribute6 uses the third column, attribute7 uses the 4th column and everything uses only this row N. and when he sees the letter P, he takes information from his line and uses attribute8 the second column, attribute9 the third column, attribute10 the fourth column and in attribute 11 the fifth column etc…
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Linq;
public class CsvData
{
public string Attribute1 { get; set; } = string.Empty;
public string Attribute2 { get; set; } = string.Empty;
public string Attribute3 { get; set; } = string.Empty;
public List<string> AdditionalElements { get; set; } = new List<string>();
public string Attribute4 { get; set; } = string.Empty;
public string Attribute5 { get; set; } = string.Empty;
public string Attribute6 { get; set; } = string.Empty;
public string Attribute7 { get; set; } = string.Empty;
public string Attribute8 { get; set; } = string.Empty;
public string Attribute9 { get; set; } = string.Empty;
public string Attribute10 { get; set; } = string.Empty;
public string Attribute11 { get; set; } = string.Empty;
public string Attribute12 { get; set; } = string.Empty;
}
class Program
{
static void Main(string[] args)
{
string csvFile = @"Копия сборник1.csv";
string[] lines = File.ReadAllLines(csvFile, Encoding.UTF8);
Console.WriteLine("CSV lines read:");
foreach (var line in lines)
{
Console.WriteLine(line);
}
List<CsvData> csvDataList = new List<CsvData>();
foreach (string line in lines)
{
string[] values = line.Split(',');
if (values.Length < 6)
{
continue;
}
CsvData csvData = new CsvData();
switch (values[0])
{
case "РЗ":
csvData.Attribute1 = values.Length > 1 ? values[1] : string.Empty;
csvData.Attribute2 = values.Length > 2 ? values[2] : string.Empty;
break;
case "Т":
csvData.Attribute3 = values.Length > 1 ? values[1] : string.Empty;
csvData.Attribute4 = values.Length > 2 ? values[2] : string.Empty;
if (values.Length > 5)
{
csvData.AdditionalElements.Add(values[5]);
}
break;
case "Н":
csvData.Attribute5 = values.Length > 1 ? values[1] : string.Empty;
csvData.Attribute6 = values.Length > 2 ? values[2] : string.Empty;
if (values.Length > 3)
{
csvData.Attribute7 = values[3];
}
break;
case "Р":
csvData.Attribute8 = values.Length > 1 ? values[1] : string.Empty;
csvData.Attribute9 = values.Length > 2 ? values[2] : string.Empty;
if (values.Length > 3)
{
csvData.Attribute10 = values[3];
}
if (values.Length > 4)
{
csvData.Attribute11 = values[4];
}
break;
default:
break;
}
csvDataList.Add(csvData);
}
Console.WriteLine("nParsed CSV data:");
foreach (var data in csvDataList)
{
Console.WriteLine($"Attribute1: {data.Attribute1}, Attribute2: {data.Attribute2}, Attribute3: {data.Attribute3}, Attribute4: {data.Attribute4}, Attribute5: {data.Attribute5}, Attribute6: {data.Attribute6}, Attribute7: {data.Attribute7}, Attribute8: {data.Attribute8}, Attribute9: {data.Attribute9}, Attribute10: {data.Attribute10}, Attribute11: {data.Attribute11}, AdditionalElements: {string.Join(", ", data.AdditionalElements)}");
}
XElement documentElement = new XElement("DOCUMENT",
new XAttribute("NAME", "=Сборник1"),
from data in csvDataList
select new XElement("RAZDEL",
new XAttribute("CODE", data.Attribute1),
new XAttribute("NAME", data.Attribute2),
new XElement("TABLE",
new XAttribute("CODE", data.Attribute3),
new XAttribute("NAME", data.Attribute4),
new XElement("SCOPEOFWORK", data.AdditionalElements.Select(e => new XElement("NAME", e))),
new XElement("NORMA",
new XAttribute("CODE", data.Attribute5),
new XAttribute("NAME", data.Attribute6),
new XAttribute("UNIT", data.Attribute7),
new XElement("RESURS",
new XAttribute("CODE", data.Attribute8),
new XAttribute("NAME", data.Attribute9),
new XAttribute("UNIT", data.Attribute10),
new XAttribute("VOLUME", data.Attribute11)
)
)
)
)
);
Console.WriteLine("nGenerated XML:");
Console.WriteLine(documentElement);
XDocument xmlDocument = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
documentElement
);
string xmlFilePath = @"сборник.xml";
xmlDocument.Save(xmlFilePath);
Console.WriteLine($"XML файл успешно создан: {xmlFilePath}");
}
}
when you run the total file, it shows like this, without information
<DOCUMENT NAME="=Сборник1">
<RAZDEL CODE="" NAME="">
<TABLE CODE="" NAME="">
<SCOPEOFWORK/>
<NORMA CODE="" NAME="" UNIT="">
<RESURS CODE="" NAME="" UNIT="" VOLUME=""/>
</NORMA>
</TABLE>
</RAZDEL>
<RAZDEL CODE="" NAME="">
...
</RAZDEL>
<RAZDEL CODE="" NAME="">
...
</RAZDEL>
<RAZDEL CODE="" NAME="">
...
</RAZDEL>
<RAZDEL CODE="" NAME="">
...
</RAZDEL>
<RAZDEL CODE="" NAME="">
...
</RAZDEL>
etc...