Create Writer.cs

This commit is contained in:
lastvoidtemplar
2021-07-07 17:51:55 +03:00
parent 0f77306a0c
commit 36978e9fe4
4 changed files with 50 additions and 5 deletions

View File

@@ -15,4 +15,8 @@
<None Remove="Model\NewFile1.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>
</Project>

View File

@@ -25,20 +25,20 @@ namespace BoerseDataConvert
public string ConvertToXml(Record record)
{
StringBuilder xmlRecord = new StringBuilder();
xmlRecord.Append($" <record id=”{count}”>\n");
xmlRecord.Append($" <record id=\"{count}\">\n");
foreach (var tagValue in record.TagsValues)
{
try
{
string tag =CheckTagValue(tagValue.Key, tagValue.Value);
xmlRecord.Append($" <{tag}>{tagValue.Value}</{tag}>\n");
xmlRecord.Append($" <{tag}>{tagValue.Value}</{tag}>\n");
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
}
xmlRecord.Append($" </record>");
xmlRecord.Append($" </record>");
count++;
return xmlRecord.ToString();
}

View File

@@ -16,7 +16,7 @@ namespace BoerseDataConvert
public Reader(string adr, string[] filesNames)
{
fileInd = 0;
reader = new StreamReader($@"{adr}/{filesNames[fileInd]}");
reader = new StreamReader($@"{adr}/{filesNames[fileInd]}", CodePagesEncodingProvider.Instance.GetEncoding(1252));
this.adr = adr;
this.filesNames = filesNames;
reader.ReadLine();
@@ -28,8 +28,9 @@ namespace BoerseDataConvert
{
fileInd++;
reader.Close();
reader = new StreamReader($@"{adr}/{filesNames[fileInd]}");
reader = new StreamReader($@"{adr}/{filesNames[fileInd]}", CodePagesEncodingProvider.Instance.GetEncoding(1252));
RecordController.NextFile(filesNames[fileInd]);
Writer.NextFile(filesNames[fileInd]);
s = reader.ReadLine();
s = reader.ReadLine();
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BoerseDataConvert
{
public class Writer
{
private static string curFilesName;
private static StreamWriter writer;
public Writer(string filesName)
{
curFilesName = filesName;
string[] file = curFilesName.Split('.').ToArray();
writer = new StreamWriter($"{file[0]}.xml");
writer.WriteLine($"<table name=”{file[0]}”>");
}
public void WriteRecord(string record)
{
writer.WriteLine(record);
}
public static void NextFile(string fileName)
{
EndFile();
curFilesName = fileName;
string[] file = curFilesName.Split('.').ToArray();
writer = new StreamWriter($"{file[0]}.xml");
writer.WriteLine($"<table name=”{file[0]}”>");
}
public static void EndFile()
{
writer.WriteLine("</table>");
writer.Close();
}
}
}