From f9dcad447b5fce0a1b87c283c0cf08e88a9bc675 Mon Sep 17 00:00:00 2001 From: lastvoidtemplar Date: Wed, 7 Jul 2021 11:31:32 +0300 Subject: [PATCH] Update RecordController.cs --- .../Controller/RecordController.cs | 66 ++++++++++++++++--- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/BoerseDataConvert/BoerseDataConvert/Controller/RecordController.cs b/BoerseDataConvert/BoerseDataConvert/Controller/RecordController.cs index d52d670..20a2120 100644 --- a/BoerseDataConvert/BoerseDataConvert/Controller/RecordController.cs +++ b/BoerseDataConvert/BoerseDataConvert/Controller/RecordController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,26 +12,75 @@ namespace BoerseDataConvert private static int count = 1; public string ConvertToXml(Record record) { - string xlmRecord = $""; + StringBuilder xlmRecord = new StringBuilder(); + xlmRecord.Append($"\n"); foreach (var tagValue in record.TagsValues) { try { - string tag = ConvertTag(tagValue.Key); - CheckValues(tagValue.Key, tagValue.Value); - xlmRecord += $""; + string tag =CheckTagValue(tagValue.Key, tagValue.Value); + xlmRecord.Append($"<{tag}>{tagValue.Value}\n"); } - catch (Exception e) + catch (ArgumentException e) { - throw; + throw new ArgumentException(e.Message); } } - return xlmRecord; + xlmRecord.Append($""); + count++; + return xlmRecord.ToString(); } private string CheckTagValue(string tag, string value) { - return ""; + string tagname=""; + StreamReader reader = new StreamReader(@"..\..\..\..\tags.txt"); + using (reader) + { + string[] tagLine= null ; + while (!reader.EndOfStream) + { + string[] line = reader.ReadLine().Split('|').ToArray(); + if (line[0] == tag) tagLine = line; + } + if (tagLine == null) throw new ArgumentException("Invalid tag!"); + tagname = tagLine[1]; + if (value != "NULL" && tagLine.Length == 3) + { + string[] valueType = tagLine[2].Split('-').ToArray(); + if (valueType.Length == 2) + { + if (value.Length > int.Parse(valueType[1])) throw new ArgumentException("Value is too long!"); + } + else + { + try + { + double.Parse(value); + } + catch (FormatException) + { + throw new ArgumentException("Value does not represent a number in a valid format!"); + } + + } + } + else + { + string[] valueRange = tagLine[3].Split('#').ToArray(); + bool countain = false; + for (int i = 0; i < valueRange.Length; i++) + { + if (valueRange[i] == value) + { + countain = true; + break; + } + } + if(!countain) throw new ArgumentException("Value is not in value range!"); + } + } + return tagname; } } }