Click here to Skip to main content
15,867,308 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.2K   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

 
GeneralGood Work Pin
DharmarajNagarajan16-Feb-09 23:36
DharmarajNagarajan16-Feb-09 23:36 
GeneralMy vote of 1 Pin
Howard Richards29-Jan-09 22:10
Howard Richards29-Jan-09 22:10 
Questionhow to convert the logfile into database Pin
salon28-May-07 21:29
salon28-May-07 21:29 
GeneralI hit a bug Pin
KevinRMacDonald9-Feb-07 8:02
KevinRMacDonald9-Feb-07 8:02 
GeneralRe: I hit a bug Pin
Member 969-Feb-07 8:17
Member 969-Feb-07 8:17 
AnswerRe: I hit a bug Pin
KevinRMacDonald9-Feb-07 11:14
KevinRMacDonald9-Feb-07 11:14 
GeneralRe: I hit a bug Pin
Member 969-Feb-07 11:48
Member 969-Feb-07 11:48 
QuestionWhy not use an Array? Pin
kheiligh6-Feb-07 4:05
kheiligh6-Feb-07 4:05 
AnswerRe: Why not use an Array? Pin
Member 969-Feb-07 12:00
Member 969-Feb-07 12:00 
GeneralGreat Work! Pin
Pengie28-Nov-06 14:44
Pengie28-Nov-06 14:44 
GeneralExcellent works right away (Here are some changes I did to use DataGridView and remove Carriage Return and Line Feeds) Pin
LuluSailor16-Aug-06 6:39
LuluSailor16-Aug-06 6:39 
QuestionNewbie - any updates regarding use of ADO.NET 2.0? Pin
LuluSailor16-Aug-06 4:15
LuluSailor16-Aug-06 4:15 
AnswerRe: Newbie - any updates regarding use of ADO.NET 2.0? Pin
Member 9616-Aug-06 6:07
Member 9616-Aug-06 6:07 
GeneralRe: Newbie - any updates regarding use of ADO.NET 2.0? Pin
LuluSailor16-Aug-06 6:29
LuluSailor16-Aug-06 6:29 
GeneralNicely done Pin
Sharpmike19-Apr-06 3:43
Sharpmike19-Apr-06 3:43 
Generalparsing out certain data in text file Pin
johnnyphaze1-Dec-05 10:14
johnnyphaze1-Dec-05 10:14 
GeneralRe: parsing out certain data in text file Pin
Member 962-Dec-05 3:02
Member 962-Dec-05 3:02 
GeneralCSV Files Pin
boomer194616-Aug-05 18:28
boomer194616-Aug-05 18:28 
AnswerRe: CSV Files Pin
Member 9617-Aug-05 5:45
Member 9617-Aug-05 5:45 
GeneralRe: CSV Files Pin
boomer194617-Aug-05 16:21
boomer194617-Aug-05 16:21 
Generalshows ur Xperience Pin
Srinivas Varukala20-Jun-05 7:55
Srinivas Varukala20-Jun-05 7:55 
GeneralThank you. Pin
Justin Keyes23-Jan-05 9:44
Justin Keyes23-Jan-05 9:44 
QuestionWhy not use a normal data connection? Pin
Thesisus10-Mar-04 4:19
Thesisus10-Mar-04 4:19 
AnswerRe: Why not use a normal data connection? Pin
Member 9610-Mar-04 5:14
Member 9610-Mar-04 5:14 
GeneralRe: Why not use a normal data connection? Pin
Thesisus10-Mar-04 5:51
Thesisus10-Mar-04 5:51 

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.