Click here to Skip to main content
13,087,235 members (54,867 online)
Click here to Skip to main content
Add your own
alternative version


86 bookmarked
Posted 8 Mar 2003

ASC2XXX - Two classes for parsing delimited text files

, 8 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Convert delimited text files to XML file or DataSet object
<!-- Add the rest of your HTML here -->

Turn a delimited text file:

Into a DataSet:

Or an XML file:


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


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.


  • Original version: Feb.26.2003


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


About the Author

Member 96
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood Work Pin
DharmarajNagarajan16-Feb-09 23:36
memberDharmarajNagarajan16-Feb-09 23:36 
GeneralMy vote of 1 Pin
Howard Richards29-Jan-09 22:10
memberHoward Richards29-Jan-09 22:10 
Questionhow to convert the logfile into database Pin
bhavna81628-May-07 21:29
memberbhavna81628-May-07 21:29 
GeneralI hit a bug Pin
KevinRMacDonald9-Feb-07 8:02
memberKevinRMacDonald9-Feb-07 8:02 
GeneralRe: I hit a bug Pin
John Cardinal9-Feb-07 8:17
memberJohn Cardinal9-Feb-07 8:17 
AnswerRe: I hit a bug Pin
KevinRMacDonald9-Feb-07 11:14
memberKevinRMacDonald9-Feb-07 11:14 
GeneralRe: I hit a bug Pin
John Cardinal9-Feb-07 11:48
memberJohn Cardinal9-Feb-07 11:48 
QuestionWhy not use an Array? Pin
kheiligh6-Feb-07 4:05
memberkheiligh6-Feb-07 4:05 
AnswerRe: Why not use an Array? Pin
John Cardinal9-Feb-07 12:00
memberJohn Cardinal9-Feb-07 12:00 
GeneralGreat Work! Pin
Pengie28-Nov-06 14:44
memberPengie28-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
memberLuluSailor16-Aug-06 6:39 
QuestionNewbie - any updates regarding use of ADO.NET 2.0? Pin
LuluSailor16-Aug-06 4:15
memberLuluSailor16-Aug-06 4:15 
AnswerRe: Newbie - any updates regarding use of ADO.NET 2.0? Pin
John Cardinal16-Aug-06 6:07
memberJohn Cardinal16-Aug-06 6:07 
GeneralRe: Newbie - any updates regarding use of ADO.NET 2.0? Pin
LuluSailor16-Aug-06 6:29
memberLuluSailor16-Aug-06 6:29 
GeneralNicely done Pin
Sharpmike19-Apr-06 3:43
memberSharpmike19-Apr-06 3:43 
Generalparsing out certain data in text file Pin
johnnyphaze1-Dec-05 10:14
memberjohnnyphaze1-Dec-05 10:14 
GeneralRe: parsing out certain data in text file Pin
John Cardinal2-Dec-05 3:02
memberJohn Cardinal2-Dec-05 3:02 
GeneralCSV Files Pin
boomer194616-Aug-05 18:28
memberboomer194616-Aug-05 18:28 
AnswerRe: CSV Files Pin
John Cardinal17-Aug-05 5:45
memberJohn Cardinal17-Aug-05 5:45 
GeneralRe: CSV Files Pin
boomer194617-Aug-05 16:21
memberboomer194617-Aug-05 16:21 
Generalshows ur Xperience Pin
Srinivas Varukala20-Jun-05 7:55
memberSrinivas Varukala20-Jun-05 7:55 
GeneralThank you. Pin
m9u3523-Jan-05 9:44
memberm9u3523-Jan-05 9:44 
QuestionWhy not use a normal data connection? Pin
Paul Menefee10-Mar-04 4:19
memberPaul Menefee10-Mar-04 4:19 
AnswerRe: Why not use a normal data connection? Pin
John Cardinal10-Mar-04 5:14
memberJohn Cardinal10-Mar-04 5:14 
GeneralRe: Why not use a normal data connection? Pin
Paul Menefee10-Mar-04 5:51
memberPaul Menefee10-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 9 Mar 2003
Article Copyright 2003 by Member 96
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid