Idea by Chris Lovett
Software Architect, WebData team, Microsoft Corporation
March 2002 (Revised February 2004)
Implementation by Ravi Rajamani

Summary: XmlCsvReader is a sample implementation of an XmlReader that parses comma separated .csv files and presents their contents as a stream of XML elements; XmlReader is the Microsoft .NET Framework base class for all managed stream-level XML processing.

XmlCsvReader Constructor

XmlCsvReader is a subclass of XmlReader so you can use it anywhere you can use the XmlReader, for example in loading XmlDocument objects. The only thing you need to understand beyond the XmlReader API itself (which is documented in the Microsoft .NET Frameworks SDK) are the XmlCsvReader constructors as follows:

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

Uri location
The location of the .csv file. It uses the XmlUrlResolver to resolve this so it could be a file or http location.
Stream input
Alternatively you can load the reader from a stream.
TextReader input
Alternatively you can load the reader from a text reader.
Uri baseUri
When loading from a stream, you also need to provide the baseUri used for resolving external entities.
XmlNameTable nametable
To use while atomizing element names.

XmlCsvReader Code Sample

Test.cs
using System.Xml;
using Microsoft.Xml;

public class Test {

public static void Main(){

XmlDocument doc = new XmlDocument();
XmlCsvReader reader = new XmlCsvReader(new Uri("file:///C:/My%20Download%20Files/XmlCsvReader/XmlCsvReader/input.csv"), doc.NameTable);
reader.FirstRowHasColumnNames = true;
reader.RootName = "customers";
reader.RowName = "customer";

doc.Load(reader);
Console.WriteLine(doc.OuterXml);
doc.Save("output.xml");
}

}

Input.csv:
"customerid","accountnum","lname","fname","mi"
1,87462024688,"Nowmer","Sheri","A."
2,87470586299,"Whelply","Derrick","I."
3,87475757600,"Derry","Jeanne",""
4,87500482201,"Spence","Michael","J."
5,87514054179,"Gutierrez","Maya",""
6,87517782449,"Damstra","Robert","F."
7,87521172800,"Kanagaki","Rebecca",""
8,87539744377,"Brunner","Kim","H."
9,87544797658,"Blumberg","Brenda","C."
10,87568712234,"Stanz","Darren","M."

Output.xml:
<customers>
<customer>
<customerid>1</customerid>
<accountnum>87462024688</accountnum>
<lname>Nowmer</lname>
<fname>Sheri</fname>
<mi>A.</mi>
</customer>
<customer>
<customerid>2</customerid>
<accountnum>87470586299</accountnum>
<lname>Whelply</lname>
<fname>Derrick</fname>
<mi>I.</mi>
</customer>
<customer>
<customerid>3</customerid>
<accountnum>87475757600</accountnum>
<lname>Derry</lname>
<fname>Jeanne</fname>
<mi>
</mi>
</customer>
<customer>
<customerid>4</customerid>
<accountnum>87500482201</accountnum>
<lname>Spence</lname>
<fname>Michael</fname>
<mi>J.</mi>
</customer>
<customer>
<customerid>5</customerid>
<accountnum>87514054179</accountnum>
<lname>Gutierrez</lname>
<fname>Maya</fname>
<mi>
</mi>
</customer>
<customer>
<customerid>6</customerid>
<accountnum>87517782449</accountnum>
<lname>Damstra</lname>
<fname>Robert</fname>
<mi>F.</mi>
</customer>
<customer>
<customerid>7</customerid>
<accountnum>87521172800</accountnum>
<lname>Kanagaki</lname>
<fname>Rebecca</fname>
<mi>
</mi>
</customer>
<customer>
<customerid>8</customerid>
<accountnum>87539744377</accountnum>
<lname>Brunner</lname>
<fname>Kim</fname>
<mi>H.</mi>
</customer>
<customer>
<customerid>9</customerid>
<accountnum>87544797658</accountnum>
<lname>Blumberg</lname>
<fname>Brenda</fname>
<mi>C.</mi>
</customer>
<customer>
<customerid>10</customerid>
<accountnum>87568712234</accountnum>
<lname>Stanz</lname>
<fname>Darren</fname>
<mi>M.</mi>
</customer>
</customers>


Last edited Jan 12, 2013 at 12:26 AM by ravibeta, version 3

Comments

No comments yet.