English 中文(简体)
记录文档和输入数据
原标题:Parse a log file and get entry data

我有多条线的大型航标,按新线分开。 每一条线入口都储存着4项价值。

如果我阅读了该日志,想存储这些信息,那么我应该使用什么样的数据类型/目标?

例:

source1 destination1 result time
source2 destination1 result time
sources3 destination2 result time

这些价值观在界线之间并不独特,可以重复。

Can 我宣布了每一条线的构件,其编号为{源、目的地、结果、时间}数值,并储存整份文件的“代码>List<T>。

最佳回答

是的,你可以采取办法,创建自己的特定类别,并制作该类的<条码>。

根据您的档案数量,你可以通过使用<代码> 可<>可<>>/代码”来做到这一点:

var dt = new DataTable();
dt.Columns.Add(new DataColumn("Source", typeof(string)));
dt.Columns.Add(new DataColumn("Destination", typeof(string)));
dt.Columns.Add(new DataColumn("Result", typeof(string)));
dt.Columns.Add(new DataColumn("Timestamp", typeof(DateTime)));

var dr = dt.NewRow();
dr["Source"] = "DATA";
dr["Destination"] = "DATA";
dr["Result"] = "DATA";
dr["Timestamp"] = DateTime.Now;
dt.Rows.Add(dr);
问题回答

这正是我要做的事情。 如果你没有准则,你可以使用通用名单,并按自己的习俗分类。

public class LogEntry
{
     private String _source = String.Empty;
     private String _destination = String.Empty;
     private String _result = String.Empty;
     private String _time = String.Empty;

     public String Source
     {
         get { return _source; }
         set { _source = value; }
     }

     public String Destination
     {
            get { return _source; }
            set { _source = value; }
     }

     public String Result
     {
         get { return _source; }
         set { _source = value; }
     }

     public String Time
     {
         get { return _source; }
         set { _source = value; }
     }

     public LogEntry()
     {
     }

     public LogEntry( String source, String destination, String result, String time )
     {
         _source = source;
         _destination = destination;
         _result = result;
         _time = time;
     }

    public LogEntry( String[] args )
    {
        _source = args[0];
        _destination = args[1];
        _result = args[2];
        _time = args[3];
    }
  }

那么,你就可以使用这样的清单:

 List<LogEntry> _logEntries = new List<LogEntry>();

我将从卷宗中读到每一行,并创设一个新式的从强令中转的LogEntry级。 辅助方法使用。 下面的法典是严格的代号。

char[] splitter = new char[1];
splitter[0] =    ;

while( readLines into String )
{
   _logEntries.Add( new LogEntry( String.Split( splitter ) ) );
}

这种解决办法极为困难,对记录格式的任何改动都将hor断该守则,但将会奏效。 同样,你打破了白色空间,使你能更好地确保你真的希望破灭。

        class YourType
        {
            public String Source { get; private set; }
            public String Destination { get; private set; }
            public String Resoult { get; private set; }
            public String Time { get; private set; }

            //or func returning bool and remove exception
            public YourType(String line)
            {
                var split = line.Split(   );

                if (split.Length != 4)
                    throw new ArgumentException();

                Source = split[0];
                Destination = split[1];
                Resoult = split[2];
                Time = split[3];
            }

        }

        public Main()
        {
            String values =
 @"source1 destination1 result time
source2 destination1 result time
sources3 destination2 result time";



            var v = from line in values.Split(new String[] { "
" }, StringSplitOptions.RemoveEmptyEntries)
                    select new YourType(line);

            foreach (var it in v)
            {
                Console.WriteLine(it.Source);
            }

        }

缩略语

public class LogEntry
{
    public string Source;
    public string Destination;
    public DateTime ResultTime;
}

然后,你可以在每个入境线上打电话给Split(),然后把Sting[]的每个部分划入你的伐木物体。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签