在将Azure Blob中的CSV文件解析为流时,TextFieldParser将始终直接到达EndOfData,而不会读取任何数据。相同的代码,但路径相同的物理文件,而不是流的作品。使用TextFieldParser从流中解析CSV始终达到EndOfData
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(AzureStorageConnection)
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim BlobList As IEnumerable(Of CloudBlockBlob) = blobClient.GetContainerReference("containername").ListBlobs().OfType(Of CloudBlockBlob)
For Each blb In BlobList
Dim myList As New List(Of MyBusinessObject)
Using memoryStream = New MemoryStream()
blb.DownloadToStream(memoryStream)
Using Reader As New FileIO.TextFieldParser(memoryStream)
Reader.TextFieldType = FileIO.FieldType.FixedWidth
Reader.SetFieldWidths(2, 9, 10)
Dim currentRow As String()
While Not Reader.EndOfData
Try
currentRow = Reader.ReadFields()
myList.Add(New GsmXFileRow() With {
' code to read currentRow and add elements to myList
})
Catch ex As FileIO.MalformedLineException
End Try
End While
End Using
End Using
Next
我也试图MemoryStream
转换为TextReader
Dim myTextReader As TextReader = New StreamReader(memoryStream)
,然后传递到myTextReader
TextFieldParser
,但这也不管用。
Using Reader As New FileIO.TextFieldParser(myTextReader)
你已经把一个断点,并检查了你的'memoryStream'有你期望的数据?另外...我看到你的Try/Catch块正在吞噬捕获到的异常。你有没有Try/Catch运行代码,看是否有错误被抛出?我怀疑将字节转换为字符串的编码问题导致MalformedLineExceptions您永远不会看到。 –
@JoelCoehoorn是的。 'Length'属性的值等于文件大小。 '位置'属性具有相同的值,表明流位于其末尾。对于异常,代码从不在'While'内执行,因为'Reader.EndOfData'总是'True'。 – Megrez7
“'位置属性具有相同的值'”。那就是你的问题。它已经读到蒸汽的尽头了。你需要重新开始。当你这样做时,请检查我的答案,因为你可能也需要解决编码问题。 –