parsing object from text file c#
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
- c# integer to bit string
- Category
- C#
- Title
- how to create and trigger a function unity animation events
- Category
- C#
- Title
- perlin noise unity
- Category
- C#
- Title
- c# stringbuilder
- Category
- C#
- Title
- unity ui not seen
- Category
- C#
- Title
- dynamically add rows to datagridview c#
- Category
- C#
- Title
- how does Pow work C#
- Category
- C#
- Title
- c# download file
- Category
- C#
- Title
- The CodeDom provider type "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" could not be located.
- Category
- C#
- Title
- c# get full URL of page
- Category
- C#
- Title
- cc# sort list with list if ids
- Category
- C#
- Title
- unity transformer double en float
- Category
- C#
- Title
- c# how to exit program
- Category
- C#
- Title
- unknown discriminator value mongodb
- Category
- C#
- Title
- asp.net core timeout
- Category
- C#
- Title
- open file in explorer c#
- Category
- C#
- Title
- c# enum.getvalues
- Category
- C#
- Title
- get hash c#
- Category
- C#
- Title
- get setter c# model
- Category
- C#
- Title
- assign datasource to dropdownlist in c#
- Category
- C#
- Title
- get enum value c#
- Category
- C#
- Title
- how to destroy a gameobject after some hits in unity 3d
- Category
- C#
- Title
- c# quaternion eular calculator
- Category
- C#
- Title
- c# md5 int
- Category
- C#
- Title
- .net core session
- Category
- C#
- Title
- math class C# exponents
- Category
- C#
- Title
- The type or namespace name 'Scrollbar' could not be found
- Category
- C#
- Title
- c# mathf.ceiling
- Category
- C#
- Title
- unity instantiate an object
- Category
- C#
- Title
- c#if
- Category
- C#
- Title
- create dropdown in datatable c# dynamically
- Category
- C#
- Title
- unity delete all children
- Category
- C#
- Title
- how to pass object as test case in nunit c#
- Category
- C#
- Title
- c# convert dictionary to anonymous object
- Category
- C#
- Title
- make string
- Category
- C#
- Title
- c# thread sleep
- Category
- C#
- Title
- C# array to string
- Category
- C#
- Title
- C# extend array
- Category
- C#
- Title
- how to add movement in unity
- Category
- C#
- Title
- calculate impact damage + unity
- Category
- C#
- Title
- set decimal point c#
- Category
- C#
- Title
- c sharp check if key in dictionary
- Category
- C#
- Title
- scaffold-dbcontext sql server
- Category
- C#
- Title
- create line in unity
- Category
- C#
- Title
- zip file ignoring directory starting with dot
- Category
- C#
- Title
- unity button interactable
- Category
- C#
- Title
- public enum c#
- Category
- C#
- Title
- c# generic abstract method
- Category
- C#
- Title
- .net core executenonqueryasync transaction
- Category
- C#
- Title
- repeater itemdatabound event in c#
- Category
- C#
- Title
- invalidoperationexception c# ui thread
- Category
- C#
- Title
- C# datareadeer return null
- Category
- C#
- Title
- how to spawn coins randomly around the screen unity 2d
- Category
- C#
- Title
- c# declare an int list
- Category
- C#
- Title
- c# return two variables of different types
- Category
- C#
- Title
- dynamic convert type c#
- Category
- C#
- Title
- C# how to get public key for InternalsVisibleTo
- Category
- C#
- Title
- unity resources load
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- c# arraylist to listview
- Category
- C#
- Title
- c# find index element array
- Category
- C#
- Title
- c list add element
- Category
- C#
- Title
- c# summary tag
- Category
- C#
- Title
- c# input integer
- Category
- C#
- Title
- weakreference tryget c#
- Category
- C#
- Title
- razor preview
- Category
- C#
- Title
- c# round number down
- Category
- C#
- Title
- c# get list of all class fields
- Category
- C#
- Title
- list contains type c#
- Category
- C#
- Title
- c sharp string interpolation
- Category
- C#
- Title
- c# how to add newline on text box
- Category
- C#
- Title
- using serial port in c#
- Category
- C#
- Title
- c# property get set
- Category
- C#
- Title
- unity randomly generated numbers c#
- Category
- C#
- Title
- C# previous method
- Category
- C#
- Title
- json tiers dot in name c#
- Category
- C#
- Title
- c# array isn't working
- Category
- C#
- Title
- Unity how to put IEnumerator in update and loop once with yeild return new waitforseconds
- Category
- C#
- Title
- wpf c# select folder path
- Category
- C#
- Title
- asp.net mvc return file
- Category
- C#
- Title
- inheritance c#
- Category
- C#
- Title
- radians to degree c#
- Category
- C#
- Title
- c# send email
- Category
- C#
- Title
- c# typeof
- Category
- C#
- Title
- c# cheat sheet
- Category
- C#
- Title
- c# list to string
- Category
- C#
- Title
- c# AllowSynchronousIO to true
- Category
- C#
- Title
- how to get value from object in c#
- Category
- C#
- Title
- how to use K2 games Games parallax background
- Category
- C#
- Title
- no overload for 'useItemOnSceneLoad' matches delegate 'UnityAction<Scene, LoadSceneMode>'
- Category
- C#
- Title
- no entity framework provider found for the ado.net provider with invariant name
- Category
- C#
- Title
- c# auto property set default value
- Category
- C#
- Title
- unity move character
- Category
- C#
- Title
- max of array C#
- Category
- C#
- Title
- import regex c#
- Category
- C#
- Title
- what is a return statement C#
- Category
- C#
- Title
- unity timer
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- isInstanceOf nunit
- Category
- C#
- Title
- c# calculate difference between two dates in days
- Category
- C#