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