parsing object from text file c#
class Program
{
static void Main(string[] args)
{
var servers = new Dictionary<string, Server>(); // parse results are here
using (var fs = File.OpenRead(@"G:\tmp\so\parse.txt")) {
using (var reader = new StreamReader(fs)) {
string line;
Server current = null;
while ((line = reader.ReadLine()) != null) {
// line break - end of server definition
if (String.IsNullOrWhiteSpace(line)) {
if (current != null) {
servers.Add(current.Name, current);
current = null;
}
continue;
}
var cidx = line.IndexOf(':'); // split by colon
if (cidx >= 0) {
var name = line.Substring(0, cidx).Trim();
var value = line.Substring(cidx + 1).Trim();
// ugly switch
switch (name) {
case "Server":
current = new Server() {Name = value};
break;
case "ERROR 0":
if (current == null)
throw new Exception("Invalid line"); // more details here, just example
current.Error0 = int.Parse(value);
break;
case "ERROR 3":
if (current == null)
throw new Exception("Invalid line"); // more details here, just example
current.Error3 = int.Parse(value);
break;
case "ERROR 4":
if (current == null)
throw new Exception("Invalid line"); // more details here, just example
current.Error4 = int.Parse(value);
break;
case "ERROR 8":
if (current == null)
throw new Exception("Invalid line"); // more details here, just example
current.Error8 = int.Parse(value);
break;
case "ERROR 9":
if (current == null)
throw new Exception("Invalid line"); // more details here, just example
current.Error9 = int.Parse(value);
break;
}
}
else {
var tdix = line.IndexOf('|');
if (tdix >= 0) {
var name = line.Substring(0, tdix).Trim();
var value = line.Substring(tdix + 1).Trim();
if (servers.ContainsKey(name)) {
servers[name].CronJobErrors.Add(value);
}
}
}
}
}
}
}
}class Server {
public string Name { get; set; }
public int Error0 { get; set; }
public int Error3 { get; set; }
public int Error4 { get; set; }
public int Error8 { get; set; }
public int Error9 { get; set; }
public List<string> CronJobErrors { get; set; } = new List<string>();
}
Also in C#:
- Title
- what does gameobject.find return
- Category
- C#
- Title
- javascript append
- Category
- C#
- Title
- c# shorten an method
- Category
- C#
- Title
- iterate through dictionary c#
- Category
- C#
- Title
- jumping with character controller unity
- Category
- C#
- Title
- c# making a folder wpf
- Category
- C#
- Title
- reload scene unity
- Category
- C#
- Title
- visual studio C# hintergrund bild ändern
- Category
- C#
- Title
- unity check tag
- Category
- C#
- Title
- asp.net mvc return file
- Category
- C#
- Title
- how to declare variables in c#
- Category
- C#
- Title
- divide string in chunks c#
- Category
- C#
- Title
- how to find the area of a circle
- Category
- C#
- Title
- linq c# where condition
- Category
- C#
- Title
- string format c#
- Category
- C#
- Title
- c# shorten an definition
- Category
- C#
- Title
- input get button uibutton
- Category
- C#
- Title
- unity how to end a game with esc
- Category
- C#
- Title
- unity get max occurrence in list
- Category
- C#
- Title
- xamarin forms alarm
- Category
- C#
- Title
- convert string to int c#
- Category
- C#
- Title
- bytes size c#
- Category
- C#
- Title
- F# convert generic.List to list
- Category
- C#
- Title
- unity image
- Category
- C#