"parsing object from text file c#" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "parsing object from text file c#" answered properly. Developers are finding an appropriate answer about parsing object from text file c# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like parsing object from text file c#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on parsing object from text file c#.
parsing object from text file c#
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>();
}
Source: stackoverflow.com
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);
}
}
}
}
}
}
}
}
Source: stackoverflow.com
All those coders who are working on the C# based application and are stuck on parsing object from text file c# can get a collection of related answers to their query. Programmers need to enter their query on parsing object from text file c# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about parsing object from text file c# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of parsing object from text file c# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "parsing object from text file c#". Visit this developer's friendly online web community, CodeProZone, and get your queries like parsing object from text file c# resolved professionally and stay updated to the latest C# updates.