我正在尝试读取一个固定宽度的文本文件并将每个元素存储到一个类对象中。文本文件中的每一行都需要检查必须执行的解析操作。第 1000 行和第 1001 行需要分别进行不同的解析,第 1002-1004 行使用相同的解析操作。我正在读取文件并正确解析,但是当我向类对象添加元素时,该对象会在每个“if”语句中被覆盖。然后,当对象被添加到列表时,列表包含空行。我觉得我采取了错误的方法。我是 C# 的新手,如果这个问题已经得到解答,我深表歉意,但经过数小时的搜索,我找不到解决方案。下面是文本文件和代码。
1000 AttributeOne AttributeTwo
1001 123456.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 1004 1234.0 2345.0 3456.0 1000 AttributeOne AttributeTwo
1001 456789.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 1004 1234.0 2345.0 3456.0 1000 AttributeOne AttributeTwo
1001 234567.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 1004 1234.0 2345.0 3456.0 1000 AttributeOne AttributeTwo
1001 345678.000000 AttributeFour
1002 1234.0 2345.0 3456.0 1003 1234.0 2345.0 3456.0 103450 12344.0.02
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
namespace list_question
{
public class someObject
{
//Line 1
public int Line1_ID;
public string Line1_Attribute1;
public string Line1_Attribute2;
//Line 2
public int Line2_ID;
public decimal Line2_Attribute3;
public string Line2_Attribute4;
//Line 3
public int Line3_ID;
public decimal data_pos1;
public decimal data_pos2;
public decimal data_pos3;
}
class Program
{
static void Main(string[] args)
{
string filepathRead = @"C:\Users\user1\Desktop\SampleFile.txt";
List<someObject> somObList = new List<someObject>();
try
{
FileStream fs = new FileStream(filepathRead, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if ((line.Substring(0, 4) == "1000"))
{
string[] strArrFields1 = Regex.Split(line, @"(.{6})" + "(.{14})" + "(.{20})").Where(s => !string.IsNullOrEmpty(s)).ToArray();
someObject obj_1 = new someObject();
obj_1.Line1_ID = int.Parse(strArrFields1[0].TrimEnd(' '));
obj_1.Line1_Attribute1 = strArrFields1[1].TrimEnd(' ');
obj_1.Line1_Attribute2 = strArrFields1[2].TrimEnd(' ');
somObList.Add(obj_1);
}
else if ((line.Substring(0, 4) == "1001"))
{
string[] strArrFields2 = Regex.Split(line, @"(.{6})" + "(.{20})" + "(.{15})").Where(s => !string.IsNullOrEmpty(s)).ToArray();
someObject obj_1 = new someObject();
obj_1.Line2_ID = int.Parse(strArrFields2[0].TrimEnd(' '));
obj_1.Line2_Attribute3 = decimal.Parse(strArrFields2[1].TrimEnd(' '));
obj_1.Line2_Attribute4 = strArrFields2[2].TrimEnd(' ');
somObList.Add(obj_1);
}
else
{
string[] strArrFields3 = Regex.Split(line, @"(.{6})" + "(.{7})" + "(.{7})" + "(.{7})").Where(s => !string.IsNullOrEmpty(s)).ToArray();
someObject obj_1 = new someObject();
obj_1.Line3_ID = int.Parse(strArrFields3[0].TrimEnd(' '));
obj_1.data_pos1 = decimal.Parse(strArrFields3[1].TrimEnd(' '));
obj_1.data_pos2 = decimal.Parse(strArrFields3[2].TrimEnd(' '));
obj_1.data_pos3 = decimal.Parse(strArrFields3[3].TrimEnd(' '));
somObList.Add(obj_1);
}
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Program Exited");
Environment.Exit(99);
}
foreach (someObject o in somObList)
{
Console.WriteLine("{0},{1},{2}\n{3},{4},{5}\n{6},{7},{8},{9}",
o.Line1_ID, o.Line1_Attribute1, o.Line1_Attribute2, o.Line2_ID, o.Line2_Attribute3, o.Line2_Attribute4, o.Line3_ID, o.data_pos1, o.data_pos2, o.data_pos3);
}
somObList.Clear();
}
}
}