I need to parse a fixed length txt file and convert it to XML. For that im trying to use the file helpers library.
My txt file looks somewhat like this. There are correct number of spaces between columns. Stackoverflow has removed them also textfiles cannot be posted here so this is the best I could do.
00000265287331ON Gaurav lath WVWDM9AJ3BW054510 665300313 20160111
00000265412333AB Kritin Kapoor WVWBA7AJ5AW423323 10100510855 20160111
00000265409491BC Gary Kasprov WVWMN9AN2AE538477 773713F 20160111
00000265723428ON ASBDHJW 3VWDX7AJ5BM008640 666760743 20160111
00003245782928AB Damn bruh 3VWDX7AJ7BM008526 10101931403 20160111
12345665842041NB Ayeyeyeye Yoyoyoy 3VWDX7AJ4BM317970 19498690 20160111
09876565877284ON Sample Name 3VWLX7AJ0BM004748 665727399 20160111
76542365887122AB Dragutiin ivanovic WVWCA7AJ7BW072789 10102120532 20160111
09843266079601BC Wayne Rooney 3VW3L7AJ4BM014663 837089F 20160111
23049466096203SK John Terry 3VWBK7AJXBM311645 300656416 20160111
My main looks like this
static void Main(string[] args)
{
var engine = new FixedFileEngine<VWDischargeTXT>();
VWDischargeTXT[] result = engine.ReadFile("C:\\Users\\GL1676\\Desktop\\CSRSDischarge01112016.txt");
foreach (var detail in result)
Console.WriteLine(" AcctNum: {0}, Province: {1}", detail.AcctNum, detail.province);
Console.Read();
}
Whenever I run the program a new windoe opens which highlights the path of the file and says
BadUsageException was unhandled. End Of Line found processing the field: XYZ at line 2. (You need to mark it as [FieldOptional if you want to avoid this exception])
When I add the field as optional(even when it is not optional in any way and is in fact very important) there are a whole lot of other errors.
EDIT: Here is the definition for VWDischargeTXT
[FixedLengthRecord()]
class VWDischargeTXT
{
[FieldFixedLength(14)]
public int AcctNum;
[FieldFixedLength(8)]
[FieldTrim(TrimMode.Right)]
public char province;
[FieldFixedLength(126)]
[FieldTrim(TrimMode.Right)]
public char CustomerName;
[FieldFixedLength(26)]
[FieldTrim(TrimMode.Right)]
public char VIN;
[FieldFixedLength(51)]
[FieldTrim(TrimMode.Right)]
public char RegNo;
[FieldFixedLength(31)]
[FieldTrim(TrimMode.Right)]
[FieldConverter(ConverterKind.Date, "yyyyMMdd")]
public DateTime DischargeDate;
}
As you pointed out, the spacing looks a little off in your question. I've edited it just to format the data as code, which looks to format it correctly (it seems well spaced).
Based on this edit, I've modified your VWDischargeTXT
class so that it will work with the data. It becomes
[FixedLengthRecord(FixedMode.AllowLessChars)]
[IgnoreEmptyLines]
class VWDischargeTXT
{
[FieldFixedLength(14)]
public long AcctNum;
[FieldFixedLength(8)]
[FieldTrim(TrimMode.Right)]
public string province;
[FieldFixedLength(137)]
[FieldTrim(TrimMode.Right)]
public string CustomerName;
[FieldFixedLength(26)]
[FieldTrim(TrimMode.Right)]
public string VIN;
[FieldFixedLength(51)]
[FieldTrim(TrimMode.Right)]
public string RegNo;
[FieldFixedLength(31)]
[FieldTrim(TrimMode.Right)]
[FieldConverter(ConverterKind.Date, "yyyyMMdd")]
public DateTime DischargeDate;
}
Some notes:
FixedMode.AllowLessChars
is added to fix an error thrown
complaining about the DischargeDate
field on line 11. This may just
be the formatting of the data in the question.IgnoreEmptyLines
has been added to skip the blank lines. These blanks might just be the way it has been pasted into the question.AcctNum
has been changed to long
, as it is too large a number to be converted to an int
.char
fields have been changed to string
as char
is only used for single characters.CustomerName
has been changed from 126
to 137
to match the data.I've updated your code to show all the fields if you want to test
static void Main(string[] args)
{
var engine = new FixedFileEngine<VWDischargeTXT>();
VWDischargeTXT[] result = engine.ReadFile("C:\\Users\\GL1676\\Desktop\\CSRSDischarge01112016.txt");
foreach (var detail in result)
Console.WriteLine(" AcctNum: {0}, Province: {1}, CustomerName: {2}, VIN: {3}, RegNo: {4}, DischargeDate: {5}",
detail.AcctNum, detail.province, detail.CustomerName, detail.VIN, detail.RegNo, detail.DischargeDate);
Console.Read();
}