Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / C#
Article

ASC2XXX - Two classes for parsing delimited text files

Rate me:
Please Sign up or sign in to vote.
3.75/5 (56 votes)
8 Mar 20032 min read 243.6K   6.7K   86   44
Convert delimited text files to XML file or DataSet object

Turn a delimited text file:

Into a DataSet:

Or an XML file:

Introduction

Two classes that illustrate one way to: read a delimited text file, parse the "fields" of data using regular expressions and move the data it into either an XML file or a DataSet object for direct use. 

.Net framework classes used:

  • System;//For strings and things
  • System.IO;//For reading and writing streams and files
  • System.Xml;//For creating and writing the XML file
  • System.Text.RegularExpressions;//For parsing the text file
  • System.Data;//to generate a DataSet

Concepts illustrated

  • Reading and writing files through stream objects
  • Parsing text using regular expressions
  • Generating a DataSet in memory from code and using it to fill a DataGrid control
  • Generating an XML file from code

Background

The reason I wrote these classes is twofold:

  • I needed to write an application that would parse a web server log file (in W3C common log format) and put that data into a SQL server database. 
  • I needed a class that I could re-use in other applications where it was necessary to move data from a CSV text file into a database.

Using the code

Although very short, the code is commented heavily throughout and contains referenced hyperlinks to the MSDN articles that explain in more detail the .Net class being used at each point in the code where relevant.

This code is set to parse a web server log file, however it can easily be modified to parse any delimited text file and I've indicated in the comments where to do so. I've also included a commented out line of an alternate regular expression that can be used to parse comma delimited text files.

A file samplelog.txt is provided with the demo which contains a test web server log file.  I have mangled the IP Addresses for privacy, however the data is straight out of an Apache server log from our web server.

I've recently started using C# after many years of working in C++ so any constructive criticism would be welcome.

History

  • Original version: Feb.26.2003

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: tab delimited files Pin
Member 9619-Jan-04 21:40
Member 9619-Jan-04 21:40 
GeneralRe: tab delimited files Pin
juliusPH19-Jan-04 22:32
juliusPH19-Jan-04 22:32 
GeneralRe: tab delimited files Pin
simona441116-Apr-04 9:38
simona441116-Apr-04 9:38 
GeneralRe: tab delimited files Pin
Member 9616-Apr-04 12:51
Member 9616-Apr-04 12:51 
GeneralRe: tab delimited files Pin
oktayy27-Aug-04 11:10
oktayy27-Aug-04 11:10 
GeneralRe: tab delimited files Pin
Anonymous25-Aug-04 14:29
Anonymous25-Aug-04 14:29 
GeneralRe: tab delimited files Pin
oktayy25-Aug-04 14:29
oktayy25-Aug-04 14:29 
GeneralRe: tab delimited files Pin
Member 9625-Aug-04 18:41
Member 9625-Aug-04 18:41 
There is nothing to "get done":

What I wrote was: "In that case you would probably want to do it the original way in the article which is to look for the separator character instead of the text between separators."

If you do it the way it is originally written in the article instead, you have full control over handling missing separators yourself.

Not to sound too harsh here, but I'm not sure why people keep asking about this because there is obviously no magic way to handle badly formed source data.

If the input data is missing some fields, it's just missing some fields, nothing you can do about that but program to handle that situation by counting the number of fields you expect as you parse each line of text and if some fields are missing you need to deal with it in your code. Either abort and warn the user or fill in something yourself.

The original way the parsing was done in the article is very handy for handling the missing fields situation. If your input data is known to be consistent with no missing fields then you can use the much faster second method I mentioned in these threads.


An election is nothing more than the advanced auction of stolen goods.<br />
- Ambrose Bierce<br />

GeneralRe: tab delimited files Pin
Offlinesurfer18-Dec-04 14:26
Offlinesurfer18-Dec-04 14:26 
GeneralGreat!!!! Pin
Anonymous18-Apr-03 5:19
Anonymous18-Apr-03 5:19 
GeneralXML/Dataset -> delimited text Pin
jadeboy9-Mar-03 22:21
jadeboy9-Mar-03 22:21 
GeneralRe: XML/Dataset -> delimited text Pin
Member 9610-Mar-03 5:20
Member 9610-Mar-03 5:20 
GeneralRe: XML/Dataset -> delimited text Pin
rohancragg20-Mar-03 2:01
rohancragg20-Mar-03 2:01 
GeneralWelcome! Pin
Rocky Moore9-Mar-03 12:27
Rocky Moore9-Mar-03 12:27 
GeneralRe: Welcome! Pin
Member 969-Mar-03 12:33
Member 969-Mar-03 12:33 
GeneralRe: Welcome! Pin
Rocky Moore9-Mar-03 20:08
Rocky Moore9-Mar-03 20:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.