SV Double Quotes

I just had a project that required us to download a file from a vendor and parse the CSV file and place it into an SQL database. Unfortunately, this is an issue in my case as the vendor is senting up the CSV file to have each row encased in double quotes. This is because their description has commas in it. To do this i had two options.

Option 1 was to code all of it by scratch and i found a very nice tutorial on how to do this here <a href="http://www.blackbeltcoder.com/Articles/files/reading-and-writing-csv-files-in-c"> Parse CSV file with Double quotes</a>

Option 2 is to use TextFieldParser in the Microsoft.VisualBasic namespace ( Yes you can implement this even in C#). Here is an example using TextFieldParser:

public void Parser()
{
string pathFile = @"C:\TestFolder\TestCatalog.txt";
string line = "";
TextFieldParser fieldParser = null;

try
{
fieldParser = new TextFieldParser(pathFile);
fieldParser.SetDelimiters(",");
string[] array = fieldParser.ReadFields();

while ((line = fieldParser.ReadLine()) != null)
{
MessageBox.Show("This line: " + line);
}

}

catch (Exception ex)
{
MessageBox.Show("Caught Exception: " + ex);
}
finally
{
if (fieldParser != null)
fieldParser.Close();
}

return stringStuff;
}

The above uses the comma as the delimiter because even though they are using the quotes, you still have the carriage return and the end of a line has no comma dictating end of row. For Example: "Gregg","Test","Software"
As you can see above, Software ends the row with no comma after the end double quote.

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in C#, Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s