读取文本文件并转换为对象

时间:2017-03-07 13:58:53

标签: c# algorithm

我试图从文本文件中读取数据,创建一些对象并使用。

我的文字内容实际上是这样的。

Server : V2JJFERPP98
ERROR 0  : 5039
ERROR 3  : 0
ERROR 4  : 1915
ERROR 8  : 0
ERROR 9  : 0

Server : V2JJFERPP99
ERROR 0  : 0
ERROR 3  : 0
ERROR 4  : 15
ERROR 8  : 0
ERROR 9  : 1

Server : V2JJFERPP100
ERROR 0  : 0
ERROR 3  : 1
ERROR 4  : 0
ERROR 8  : 0
ERROR 9  : 0

V2JJFERPP98 | Error parsing database
V2JJFERPP100 | can't create lof file
V2JJFERPP100 | can't read backup_01_03_2017

基本上,我的目标是在文件中获得与错误报告一样多的对象。然后,在第二部分得到所有cronjob错误。

我已经为第一部分创建了一个对象,就像这样。

private string server { get; set; }
private int error0 { get; set; }
private int error3 { get; set; }
private int error4 { get; set; }
private int error8 { get; set; }
private int error9 { get; set; }

但是现在,我不知道如何继续获取对象。

首先,我在考虑阅读整个文件,并将所有内容放入var中,然后使用正则表达式检查内容。

我还检查了读取每一行的可能性,并根据行开头,开始创建一个对象。

对于这两种方法,我都不知道如何做到这一点。

我尝试了很多东西,但我认为我100%折扣,sicne它听起来并不复杂,但找不到合适的方法。

string [] lines = File.ReadAllLines(missionFilePath);

foreach (string line in lines)
{
    // for each line, we gonna create a new MissionState object
    if (line.StartsWith("Serveur"))
    {
        // Server : V2JJFERPP98
        var server = line.Substring(9);
        // and now, how to get error, and know when the new/next object come?
    }
}

1 个答案:

答案 0 :(得分:0)

如果你只有一个结构要解析而只想完成一项工作 - 你可以自己解析它,因为它非常简单。否则,请寻找第三方解决方案来帮助您。以下是如何解析文件的示例。首先定义一个类:

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

然后马上解析:

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);
                            }
                        }
                    }
                }
            }
        }            
    }        
}
相关问题