将文件读入List(Of T)

时间:2017-12-08 20:28:54

标签: vb.net csv

我对理解IList有一个问题:

在具有固定大小的文本文件中给出了一系列结构化记录。简单函数“GetFileReferences”正在读取单行并将处理后的字段映射到一组映射字段,例如clsFileImportMappingClass中定义的字符长度。 :)现在,在最好的情况下,GetFileReferences返回一个Dictionary(Of Integer,List(Of clsFileImportMappingClass)),它覆盖给定的文件并在每行中包含List(Of clsFileImportMappingClass)的n个元素。

示例1): 文件Sample1.CSV可能包含以下行

  1. 10 1
  2. 20 1
  3. GetFileReferences返回正确的结果:

    1. Key:10 |领域:02 |内容:1
    2. Key:20 |领域:02 |内容:3
    3. 例2) 文件Sample2.CSV现在包含以下行:

      1. 10 1
      2. 10 2
      3. 20 3
      4. 再次跟踪已处理的文件和变量将显示以下输出。

        错误:

        Mapping 1-> 1. Key: 10 | FIELD:02 | CONTENTS: 2 (not 1 as expected) 
        Mapping 1-> 2. Key: 10 | FIELD:02 | CONTENTS: 2 
        Mapping 2-> 3. Key: 20 | FIELD:02 | CONTENTS: 3
        

        是否有人可能知道为什么忽略具有相同映射的行。 我选择了以下方法来映射文件中的单行,其中映射存储在数据库中:

        Public Function MappingTheCSVLine(ByVal csvline As String, ByVal iMapping As List(Of clsFileImportMappingClass)) As List(Of clsEDI_FileImportMappingClass)
        
            Dim TList As New List(Of clsFileImportMappingClass)
        
            For row As Integer = 0 To iMapping.Count - 1
                Dim oField As New clsFileImportMappingClass
                oField = iMapping(row)
        
                If oField.USED Then
                    Dim fldLength As Int32
                    Dim fldFROM As Int32
        
                    fldLength = oField.FLD_LENGTH
                    fldFROM = oField.FLD_FROM
        
                    Dim sField As String
                    sField = csvline.Substring(fldFROM - 1, fldLength)
        
                    If sField.Contains("""") Then
                        sField.Replace("""", String.Empty)
                    End If
                    oField.Contents = Trim(CType(IIf(sField IsNot Nothing, sField, ""), String))
        
                    TList.Add(oField)
                End If
        
            Next
        
            'TList is returning the mapped line completely as desired
        
            Return TList
        End Function
        
        
        
        
        
        Public Function GetFileReferences(ByVal FileMapping As List(Of clsFileImportMappingClass)) As Dictionary (Of Integer, List(Of clsFileImportMappingClass))
        
            Dim EOL As String = vbCrLf
        
            Dim strText As String = System.IO.File.ReadAllText(_FileName)
        
            Dim _lines As String()
            _lines = Split(strText, EOL)
        
            Dim tDictMapping As New Dictionary(Of String, List(Of clsFileImportMappingClass))
            tDictMapping = getMappingToDictionary(FileMapping)
        
            'The object FileMapping is a List(Of clsFileImportMappingClass) and is providing general information of the file
            'which is to read. Mapping information like field lengths is stored in our local database.
            'In other words: File Mapping is the presentation of the structure of each file column described in a specific datatable.
            ' 
            'Example 2)
            '
            'File Sample2.CSV may contain following sample lines
            '10     1
            '10     2
            '20     3
        
            Dim FileRefs As New Dictionary (Of Integer, List(Of clsFileImportMappingClass))
            Dim ij As Integer : ij = 0
            For row As Integer = 0 To _lines.Count - 1
                Dim Key As String = ""
                Dim line As String : line = ""
                Dim sField As String : sField = ""
                line = _lines(row)
        
                Dim sKey As String = LTrim(RTrim(line.Substring(0, 3)))
        
                 If tDictMapping.ContainsKey(Key) Then
                    Console.WriteLine(line)
                    Console.WriteLine("Schlüssel-Wert:" & Key)
        
                    Dim oList As New List(Of clsFileImportMappingClass)
                    oList = MappingTheCSVLine(_lines(row), tDictMapping(Key))
                    tFileRefs.Add(ij, oList)
                    DebugItems(FileRefs(ij))
                    ij += 1
                End If
        
            Next
        
            Console.WriteLine("")
        
            '
            'Looping the local temporary var FileRefs again is giving me now the following, annoying wrong output. The Mapping is correct, meanwhile.
            '
        
            'KEY: 10 | FIELD:02 | CONTENTS: 2 (should be 1 !!!!!!!)
            'KEY: 10 | FIELD:02 | CONTENTS: 2
            'KEY: 20 | FIELD:02 | CONTENTS: 3
        
            For ji As Integer = 0 To FileRefs.Count - 1
        
                DebugItems(tFileRefs(ji))
        
            Next
        
            'Applying the same mapping, in my case would the mapping be with ID 10, always leads to the same scenario where the last mapped line is overwriting the other pairs (to any different lines).
        
            Return tFileRefs
        
        End Function
        
        Private Sub DebugItems(ByVal RefsList As List(Of clsFileImportMappingClass))
            For col As Integer = 0 To RefsList.Count - 1
                Dim sout As String : sout = ""
                Dim obj As New clsFileImportMappingClass : obj = RefsList(col)
                sout = "Key: " & obj.Key & " | FIELD:" & obj.FIELD_NR & " | CONTENTS: " & obj.CONTENTS.ToString
                Console.WriteLine(sout)
            Next
        End Sub
        

0 个答案:

没有答案