none
Reading a CSV file with Streamreader RRS feed

  • Question

  • I'm trying to read a csv file with a streamreader and filestream like this:

     using (StreamReader streamreader = new StreamReader(FileStream))
                {
    while ((line = streamreader.ReadLine())
                    {        
                        row = line.Split(',');
    
    etc, etc.
    }

    When I get to the 3rd row of data, it looks like this

    ,"New York, NY","My Address"

    After the word My there's hard return. When viewing non-printable characters in ms word, I can see a paragraph symbol immediately after My.

    So what's happening is that the readline() method is putting everything up to and including My in one string and everything after it in another string, but I don't know when this is going to happen in the csv. How would I account for this so that everything is stored in one string even if this occurs?

    Thanks

    Friday, May 11, 2012 6:06 PM

Answers

  • Hello,

    use StringSplitOptions.RemoveEmptyEntries in Your split..

    And declare all remove conditions in: New String() {vbTab, "     "}

    my post: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/3a6bc2a2-5ac3-4f38-b51f-f482feab5f13

    vbTab is VisualBasic Namespace. I have no knowledge how to do this in C#

    I hope this helps

    regards Ellen

    _str = sr.ReadLine.Split(New String() {vbTab, "     "}, StringSplitOptions.RemoveEmptyEntries)


    Ich benutze/ I'm using VB2008 & VB2010

    Friday, May 11, 2012 6:36 PM
  • If you're going to be handling multiple lines within a csv line, you're going to have a to a lot of patching together on your side. The StreamReader is only going to read a line at a time, it's not going to be looking into the contents of the lines to see if the ending line character was surrounded by quotes. You're going to need to have some sort of piecing back together logic, that would create a line out of two line halves, by whatever rules that csv you're reading in was created by.

    Friday, May 11, 2012 8:53 PM

All replies

  • Hello,

    use StringSplitOptions.RemoveEmptyEntries in Your split..

    And declare all remove conditions in: New String() {vbTab, "     "}

    my post: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/3a6bc2a2-5ac3-4f38-b51f-f482feab5f13

    vbTab is VisualBasic Namespace. I have no knowledge how to do this in C#

    I hope this helps

    regards Ellen

    _str = sr.ReadLine.Split(New String() {vbTab, "     "}, StringSplitOptions.RemoveEmptyEntries)


    Ich benutze/ I'm using VB2008 & VB2010

    Friday, May 11, 2012 6:36 PM
  • If you're going to be handling multiple lines within a csv line, you're going to have a to a lot of patching together on your side. The StreamReader is only going to read a line at a time, it's not going to be looking into the contents of the lines to see if the ending line character was surrounded by quotes. You're going to need to have some sort of piecing back together logic, that would create a line out of two line halves, by whatever rules that csv you're reading in was created by.

    Friday, May 11, 2012 8:53 PM