Click here to Skip to main content
6,594,932 members and growing! (15,829 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

ASC2XXX - Two classes for parsing delimited text files

By John C

Convert delimited text files to XML file or DataSet object
C#, Windows, .NET 1.0, DBA, Dev
Posted:8 Mar 2003
Views:170,823
Bookmarked:73 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
56 votes for this article.
Popularity: 6.60 Rating: 3.77 out of 5
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

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

John C


Member

Location: Canada Canada

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
GeneralGood Work PinmemberDharmarajNagarajan0:36 17 Feb '09  
GeneralMy vote of 1 PinmemberHoward Richards23:10 29 Jan '09  
Generalhow 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  
GeneralNewbie - any updates regarding use of ADO.NET 2.0? PinmemberLuluSailor5:15 16 Aug '06  
GeneralRe: 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  
GeneralWhy not use a normal data connection? PinmemberPaul Menefee5:19 10 Mar '04  
GeneralRe: 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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Mar 2003
Editor: Barry Lapthorn
Copyright 2003 by John C
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project