Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析逻辑,C#-Exception System.ArgumentOutOfRangeException_C#_Parsing_Logic - Fatal编程技术网

解析逻辑,C#-Exception System.ArgumentOutOfRangeException

解析逻辑,C#-Exception System.ArgumentOutOfRangeException,c#,parsing,logic,C#,Parsing,Logic,我有一个巨大的数据文件,看起来有点像这样: TRANSACTION NUMBER AMOUNT CNCY SVC DISPOSITION DATE/TIME % PT FT USER LOC --------------------------------------------------------------------------------------------------

我有一个巨大的数据文件,看起来有点像这样:

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     
我要做的是使用值系统和101.0,因为它们对于每个
事务编号都是一致的。当我发现这一点时,我正在使用
substring
函数来分离每个细节。现在我只是担心从CA1507开始的线路。。。。38078…..系统…..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;
if(string.IsNullOrWhiteSpace(line)| | line.Length<22)
继续;

我解析测试文件已有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();
       }
    }
}


​

谢谢你的快速回复。但是当我使用
if(IsTxnSection){if(string.IsNullOrWhitespace(line)| | line.Length<22)continue;TransactionNo=line.Substring(0,22).Trim();Console.WriteLine(“TrnNo”,TransactionNo);}
表示字符串不包含IsNullorWhitespace的定义。
字符串不包含IsNullorWhitespace的定义,或者你把它搞错了(我看到你用了“或”小写,它必须是“或”),或者你正在使用低于3.5的框架,如果它是框架,请使用IsNullOrEmpty而不是实际上,空格的S应该是大写的。谢谢Gusman!耶,是的,对不起。。。这就是在VS Jdweng外部编写代码的问题,我不使用带有事务号的行STARTSWITS的唯一原因是因为它在整个文件中只出现一次,因此存在if条件。仅发布一小部分文本很难确定确切的逻辑。我无法判断文件是否包含多个事务。使用枚举和switch语句使代码更易于阅读和理解。不查找事务号会使忽略这一行变得更加困难,并可能导致解析错误,就像您所看到的一样。