using System.Collections.Generic;
using System.Text.RegularExpressions;
public abstract class EDSSection
public string SectionName { get; set; }
public SectionType SectionType { get => Enum.TryParse(SectionName, out SectionType sectionType) ? sectionType : SectionType.Unknown; }
private Dictionary<string, string> SectionToDictionary(string section)
var sectionLines = section.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
var toReturn = new Dictionary<string, string>();
SectionName = Regex.Replace(sectionLines[0], @"[\[\]]", string.Empty);
sectionLines = sectionLines.Skip(1).ToArray();
foreach (var line in sectionLines)
var splitLine = line.Split('=').Select(s => s.Trim()).ToArray();
var value = splitLine[1];
toReturn.Add(key, value);
protected abstract object FromDictionary(Dictionary<string, string> keyValuePairs);
public object FromString(string section) => FromDictionary(SectionToDictionary(section));
public interface ISection {}
public class FileInfoSection : EDSSection, ISection
public FileInfoSection() => SectionName = "FileInfo";
public FileInfoSection(string fromString) {
SectionName = "FileInfo";
public string FileName { get; set; }
public byte FileVersion { get; set; }
public byte FileRevision { get; set; }
public string EDSVersion { get; set; } = "4.0";
public string Description { get; set; }
public DateTime CreationDateTime { get; set; }
public string CreatedBy { get; set; }
public DateTime ModificationDateTime { get; set; }
public string ModifiedBy { get; set; }
protected override FileInfoSection FromDictionary(Dictionary<string, string> keyValuePairs)
FileName = keyValuePairs["FileName"];
FileVersion = byte.Parse(keyValuePairs["FileVersion"]);
FileRevision = byte.Parse(keyValuePairs["FileRevision"]);
EDSVersion = keyValuePairs["EDSVersion"];
Description = keyValuePairs["Description"];
CreationDateTime = DateTime.Parse($"{keyValuePairs["CreationDate"]} {keyValuePairs["CreationTime"]}");
CreatedBy = keyValuePairs["CreatedBy"];
ModificationDateTime = DateTime.Parse($"{keyValuePairs["ModificationDate"]} {keyValuePairs["ModificationTime"]}");
ModifiedBy = keyValuePairs["ModifiedBy"];
public static void Main()
var str = "[FileInfo]\r\nFileName=bt70980a_v1.eds\r\nFileVersion=1\r\nFileRevision=1\r\nLastEDS=C:\\Users\\bturner\\Desktop\\BT70980A CANOpen Files\\BT70980A.eds\r\nEDSVersion=4.0\r\nDescription=\r\nCreationTime=9:20AM\r\nCreationDate=06-08-2020\r\nCreatedBy=Bradley Turner\r\nModificationTime=10:57AM\r\nModificationDate=06-08-2020\r\nModifiedBy=";
FileInfoSection f = new FileInfoSection(str);
Console.WriteLine(JsonConvert.SerializeObject(f));