[英]Parsing Logic, C# - Exception System.ArgumentOutOfRangeException
我有一個巨大的數據文件,看起來像這樣:
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();
你有這個:
TransactionNo = line.Substring(0, 22).Trim();
如果該行短於22個字符,則會引發該異常。 如果較短,請跳過該行:
if(string.IsNullOrWhiteSpace(line) || line.Length < 22)
continue;
我解析測試文件已有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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.