Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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:	mpsoparse.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);
                            }
                        }
                    }
                }
            }
        }            
    }        
}
Comment

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>();
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: true false when key pressed in c sharp unity 
Csharp :: how to execute a function in every object of list c# 
Csharp :: IOS app crashing on ios 15 unity 
Csharp :: close windows by esc wpf 
Csharp :: how to store more precise data then float c# 
Csharp :: moment script unity 
Csharp :: .net check connection 
Csharp :: unity debug log gameobject 
Csharp :: how to check if string from textbox exists in db 
Csharp :: DefaultContractResolver .net exclude null values JsonSerializerSettings ContractResolver DefaultContractResolver 
Csharp :: check if app have administrator rights c# 
Csharp :: delegates in c# 
Csharp :: c# compare 2 binary files 
Csharp :: unity random.insideunitcircle 
Csharp :: C# Character and String Literals 
Csharp :: cassandra Keyspaces repository .net 
Csharp :: change character velocity unity 
Csharp :: In ASP.NET Core how check if request is local 
Csharp :: c# switch two values 
Csharp :: how to get c# code of aspx page 
Csharp :: wpf create rectangle c# 
Csharp :: html tag inside razor tag 
Csharp :: create file gz c# 
Csharp :: lizzo net worth 
Csharp :: netmath hack 
Csharp :: method declaration in c# 
Csharp :: secret 
Csharp :: copy properties from two subclasses c# 
Csharp :: convert array to datatable c# 
Csharp :: c# online code editor 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =