Click here to Skip to main content
Licence 
First Posted 8 Mar 2003
Views 193,181
Downloads 5,688
Bookmarked 83 times

ASC2XXX - Two classes for parsing delimited text files

By Member 96 | 8 Mar 2003
Convert delimited text files to XML file or DataSet object
8 votes, 14.3%
1
4 votes, 7.1%
2
2 votes, 3.6%
3
5 votes, 8.9%
4
37 votes, 66.1%
5
4.35/5 - 56 votes
8 removed
μ 3.77, σa 2.66 [?]

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

About the Author

Member 96



Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood Work PinmemberDharmarajNagarajan0:36 17 Feb '09  
GeneralMy vote of 1 PinmemberHoward Richards23:10 29 Jan '09  
Questionhow to convert the logfile into database Pinmemberbhavna81622:29 28 May '07  
GeneralI hit a bug PinmemberKevinRMacDonald9:02 9 Feb '07  
GeneralRe: I hit a bug PinsupporterJohn Cardinal9:17 9 Feb '07  
AnswerRe: I hit a bug PinmemberKevinRMacDonald12:14 9 Feb '07  
GeneralRe: I hit a bug PinsupporterJohn Cardinal12:48 9 Feb '07  
QuestionWhy not use an Array? Pinmemberkheiligh5:05 6 Feb '07  
AnswerRe: Why not use an Array? PinsupporterJohn Cardinal13:00 9 Feb '07  
GeneralGreat Work! PinmemberPengie15:44 28 Nov '06  
GeneralExcellent works right away (Here are some changes I did to use DataGridView and remove Carriage Return and Line Feeds) PinmemberLuluSailor7:39 16 Aug '06  
QuestionNewbie - any updates regarding use of ADO.NET 2.0? PinmemberLuluSailor5:15 16 Aug '06  
AnswerRe: Newbie - any updates regarding use of ADO.NET 2.0? PinsupporterJohn Cardinal7:07 16 Aug '06  
GeneralRe: Newbie - any updates regarding use of ADO.NET 2.0? PinmemberLuluSailor7:29 16 Aug '06  
GeneralNicely done PinmemberSharpmike4:43 19 Apr '06  
Generalparsing out certain data in text file Pinmemberjohnnyphaze11:14 1 Dec '05  
GeneralRe: parsing out certain data in text file PinsupporterJohn Cardinal4:02 2 Dec '05  
GeneralCSV Files Pinmemberboomer194619:28 16 Aug '05  
AnswerRe: CSV Files PinsupporterJohn Cardinal6:45 17 Aug '05  
GeneralRe: CSV Files Pinmemberboomer194617:21 17 Aug '05  
Generalshows ur Xperience PinmemberSrinivas Varukala8:55 20 Jun '05  
GeneralThank you. Pinmemberm9u3510:44 23 Jan '05  
QuestionWhy not use a normal data connection? PinmemberPaul Menefee5:19 10 Mar '04  
AnswerRe: Why not use a normal data connection? PinsupporterJohn Cardinal6:14 10 Mar '04  
GeneralRe: Why not use a normal data connection? PinmemberPaul Menefee6:51 10 Mar '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120209.1 | Last Updated 9 Mar 2003
Article Copyright 2003 by Member 96
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid