.net 为什么TextFieldParser.ReadField会从字段中间删除连续的换行符?

.net 为什么TextFieldParser.ReadField会从字段中间删除连续的换行符?,.net,vb.net,parsing,string,file-io,.net,Vb.net,Parsing,String,File Io,我正在使用VB.NET的TextFieldParser(Microsoft.VisualBasic.FileIO.TextFieldParser)读取带分隔符的文件。但是,当我尝试在字段中读取具有连续换行符的字段时,连续换行符将转换为单个新行。我想保留连续的换行符,但不确定如何保留 下面是一个示例文件,我正在读取其中的一个字段。引号是文件内容的一部分,有三个换行符(包括第2行后面的两个连续换行符): 下面是我用来解析和读取文件的代码: Dim reader as New Microsoft.Vi

我正在使用VB.NET的TextFieldParser(Microsoft.VisualBasic.FileIO.TextFieldParser)读取带分隔符的文件。但是,当我尝试在字段中读取具有连续换行符的字段时,连续换行符将转换为单个新行。我想保留连续的换行符,但不确定如何保留

下面是一个示例文件,我正在读取其中的一个字段。引号是文件内容的一部分,有三个换行符(包括第2行后面的两个连续换行符):

下面是我用来解析和读取文件的代码:

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)
下面是“line”变量的内容。请注意,现在只有两个换行:

This is line 1
This is line 2
This is line 4, which follows two consecutive newlines.
如何保存连续的换行符?

首先,根据MSDN,将忽略空行:

如果ReadFields遇到空行, 它们被跳过,下一个被跳过 返回非空行

我相信您需要做的是使用ReadLine,然后循环查看结果

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

你可以看看“LineNumber”属性

(c#)

var beforeRead=\u parser.LineNumber;
_parser.ReadFields();
var afterRead=_parser.LineNumber;

如果(阅读后)我有相同的问题,并已在此处向Microsoft提交了错误报告:
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
var beforeRead = _parser.LineNumber;
_parser.ReadFields();
var afterRead = _parser.LineNumber;

if(afterRead <= -1)
   lineNumber = beforeRead;
else                    
   lineNumber = afterRead - 1;

for (var blankLines = beforeRead; blankLines < afterRead-1; blankLines++)
{
    Console.WriteLine();
}