XmlCsvReader

XmlCsvReader is an implementation of the .NET managed System.Xml.XmlReader base class which parses tab delimited .csv files and returns the data as a stream of XML elements.  Both the source code for the class and a command line executable are provided.

Download the zip file including the standalone executable and the full source code: XmlCsvReader.zip

See online demo at demo.aspx.
See also online source.

Command Line Usage

The command line executable version has the following options:

XmlCsvReader [-a] [-c] [-root name] [-row name] [-names a,b,c,...] customer.csv result.xml
-aSpecifies that you want attributes instead of subelements.
-cSpecifies that the first row contains column names.
-d char

Specifies the column delimiter character (default is 'auto' which means auto-detect the delimiter).  Other possibilities are -d "," and -d "|" and -d "\t".

-root nameSpecifies the root element name (default is 'root').
-row nameSpecifies the row element name (default is 'row').
-names a,b,c Specifies the actual column names to use, separated by commas or spaces. If you also specify "-c" then the header row in the .csv file is simply ignored, and your custom column names are used instead.
customer.csvThe first file name specifies the .csv input file.  If no file names are specified then it reads from standard input.
result.xmlThe second file name specifies the output xml file.  If no second file name is specified then it writes to standard output.

XmlCsvReader Constructor

XmlCsvReader is a subclass of XmlReader so you can use it anywhere you can use the XmlReader, for example in loading an XmlDocument or DataSet.  The XmlCsvReader has the following constructors:

public XmlCsvReader();
public XmlCsvReader( Uri location, 
XmlNameTable nametable);
public XmlCsvReader( Stream input,
Uri baseUri,
XmlNameTable nametable);
public XmlCsvReader( TextReader input,
Uri baseUri,
XmlNameTable nametable);

If you use the default constructor you must provide an input source either via the Href property or the TextReader property.  You can customize the output XML via the following properties:

public string RootName
The name of the root element, since .csv files do not define this. The default is "root".
public string RowName
The name of the row element , since .csv files do not define this either. The defaulyt is "row".
public bool FirstRowHasColumnNames
Indicates whether to get the column names from the first row of the .csv file.  If this is false, then the first row is returned as data and the columns are automaticalled named "a1, a2, a3...an". The default is false.
public bool ColumnsAsAttrs
Whether to return the columns as attributes of the row element or as child elements.  The default is false.
public string[] ColumnNames
Specifies the actual column names to use.  This can also return the column names found when FirstRowHasColumnNames was set to true.  If FirstRowHasColumnNames is true and column names are provided then the header row is simply ignored.
public char Delimiter
Gets or sets the column delimiter. Default is '\0' which means /// the reader will auto detect the delimiter.

Support

Please email bugs, feedback and/or feature requests to Chris Lovett.

Change History

Version Date Description
1.2 January 16th 2004 Fix handling of column names that are illegal XML names (for example, name contains a space).  Added test suite.
1.1 June 21st 2003 Fix bugs in handling of string literals. Fixed returning of attributes when loading XmlDocument 
1.0.3 April 27th 2003 Fix NameTable issues.
1.0.2 April 20th 2003 Add manual setting for column delimiter.
1.0.1 March 10th 2003 Fix bug in loading XmlCsvReader into an XmlDocument
1.0.1105 January 10th 2003 Add support for setting custom column names.
1.0 August 15th 2002 Initial version