Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TextFieldParser.ReadField remove consecutive newlines from middle of a field?

I am using VB.NET's TextFieldParser (Microsoft.VisualBasic.FileIO.TextFieldParser) to read a delimited file. However, when I try to read in a field with consecutive newlines within the field, the consecutive newlines are turned into a single new line. I would like the consecutive newlines to be preserved, but am unsure how.

Here's an example file that I am reading in with exactly one field. The quotes are part of the file's content and there are three newlines (including the two consecutive newlines following line 2):

"This is line 1
This is line 2

This is line 4, which follows two consecutive newlines."

Here is the code that I am using to parse and read in the file:

Dim reader as New Microsoft.VisualBasic.FileIO.TextFieldParser(myFile, System.Text.Encoding.Default)
reader.TextFieldType = FileIO.FieldType.Delimited
reader.SetDelimiters(",")

Dim fields As String() = reader.ReadFields
Dim line As String = fields(0)

And here is the content of the "line" variable. Note that there are only two newlines now:

This is line 1
This is line 2
This is line 4, which follows two consecutive newlines.

What can I do to preserve the consecutive newlines?

like image 824
sparks Avatar asked Nov 16 '10 19:11

sparks


1 Answers

First, according to MSDN http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.readfields.aspx blank lines are ignored:

If ReadFields encounters blank lines, they are skipped and the next non-blank line is returned.

I believe what you are going to need to do is use ReadLine http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.readline.aspx and then loop through the results.

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {","}
    Dim currentRow As String
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadLine()
            'Manipulate line...
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
        End Try
    End While
End Using
like image 171
Wade73 Avatar answered Sep 21 '22 14:09

Wade73