Friday, January 25, 2013

 

.NET: Parse Structured Text Files

I wasn’t aware of the handy TextFieldParser class, that parses a text file in a reader oriented way:

The TextFieldParser object provides methods and properties for parsing structured text files. Parsing a text file with the TextFieldParser is similar to iterating over a text file, while the ReadFields method to extract fields of text is similar to splitting the strings.

Maybe because it’s in the Microsoft.VisualBasic.FileIO namespace… How to: Read From Comma-Delimited Text Files in Visual Basic

Sure, you can write one yourself, but it’s handy for quick spikes:

using (var reader = new TextFieldParser(@"c:\logs\bigfile"))
{
reader.TextFieldType = FieldType.Delimited;
reader.Delimiters = new [] { Constants.vbTab };

while (!reader.EndOfData)
{
try
{
// If ReadFields encounters blank lines, they are skipped and next non-blank line is returned.
string[] currentRow = reader.ReadFields();

// Process row....
foreach (string currentField in currentRow)
{

}

}
catch (MalformedLineException ex)
{
// Handle malformed line....
}
}
}


    

Powered by Blogger