我有一个巨大的数据文件,看起来有点像这样:
TRANSACTION NUMBER AMOUNT CNCY SVC DISPOSITION DATE/TIME % PT FT USER LOC
------------------------------------------------------------------------------------------------------------------------------------
CA150723052447000I0002 38,078.100 CAD BOK AUTO HOLD 23 JUL 15 17:19:53 100.0 80.0 101.0 SYSTEM IBD
CA150723052447000I0002 - User Actions and Comments:
User Location Disposition Date/Time Comments
------------------------------------------------------------------------------------------------------------------------------------
IBDGWYNETH IBD MANUAL INVQ 23 JUL 15 17:20:29 inv
------------------------------------------------------------------------------------------------------------------------------------
CA150724020822000I0002 36,106.000 CAD BOK AUTO HOLD 24 JUL 15 08:19:32 100.0 80.0 101.0 SYSTEM IBD
CA150724020822000I0002 - User Actions and Comments:
User Location Disposition Date/Time Comments
------------------------------------------------------------------------------------------------------------------------------------
IBDADAM IBD MANUAL INVQ 24 JUL 15 08:25:17 investigate
我要做的是使用值SYSTEM和101.0,因为它们对于每个Transaction Number
是一致的。当我发现这个时,我使用substring
函数来分隔每个细节。现在我只是担心从CA1507开始的行.... 38,078 ..... SYSTEM ..... IBD并分离所有属性。
到目前为止,我的代码如下。它抛出异常ArgumentOutOfRange
。向下钻取,变量行的值为“”“,导致此异常。关于如何解决这个问题的任何想法?
感谢。
Boolean IsTxnSection = false;
StreamReader file = new StreamReader(@"C:\.....);
while ((line = file.ReadLine()) != null)
{
//Transaction details
if (IsTxnSection)
{
TransactionNo = line.Substring(0, 22).Trim();
Console.WriteLine("TrnNo", TransactionNo);
}
if (line.Contains("101.0") && line.Contains("SYSTEM") && line.StartsWith("CA150"))
{
IsTxnSection = true;
}
Thread.Sleep(100);
counter++;
if (counter == 100)
break;
} // end of while
file.Close();
答案 0 :(得分:0)
你有这个:
TransactionNo = line.Substring(0, 22).Trim();
如果该行短于22个字符,则会引发该异常。如果它更短,只需擦除线条:
if(string.IsNullOrWhiteSpace(line) || line.Length < 22)
continue;
答案 1 :(得分:0)
我已经解析了40年的测试文件。这是我一直在做的事情
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
enum Section
{
NONE,
TRANSACTION,
USER
}
static void Main(string[] args)
{
Section section = Section.NONE;
string TransactionNo = "";
StreamReader file = new StreamReader(FILENAME);
string line = "";
while ((line = file.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0 && !line.StartsWith("---------------"))
{
//Transaction details
switch (section)
{
case Section.NONE:
if(line.StartsWith("TRANSACTION NUMBER"))
{
section = Section.TRANSACTION;
}
break;
case Section.TRANSACTION:
if (!line.Contains("User Actions and Comments:"))
{
TransactionNo = line.Substring(0, 22).Trim();
Console.WriteLine("TrnNo : {0}", TransactionNo);
}
else
{
section = Section.USER;
}
break;
case Section.USER:
break;
}
}
}//end of while
file.Close();
}
}
}