2016-11-30 32 views
-1

我有一個文本文件,看起來像下面的格式。我正嘗試創建一個格式不正確的數據表。我已經嘗試了一些不同的東西,以正確格式化它但是我有的ReadLine問題vb.net textfile ReadLine循環

Dim SectionLineONE as string 
Dim NextSectionLine As String 

srReader = File.OpenText(MyFile) 

      Dim SectionLineONE As String 
    Do 
     SectionLineONE = srReader.ReadLine() 

      If SectionLineONE Is Nothing Then Exit Do 
      If SectionLineONE.Contains("1.0") Then 

       Dim NextSectionLine As String 
       Do 

        NextSectionLine = srReader.ReadLine() 

        If NextSectionLine Is Nothing Then Exit Do 
        If NextSectionLine.Contains("1.1") Then 

         Dim NextSectionLine3 As String 
         Do 

          NextSectionLine3 = srReader.ReadLine() 

          If NextSectionLine Is Nothing Then Exit Do 
         If NextSectionLine.Contains("1.1.1") Then 

          Dim NextSectionLine4 As String 
          Do 

           NextSectionLine4 = srReader.ReadLine() 

           If NextSectionLine Is Nothing Then Exit Do 
           If NextSectionLine.Contains("1.1.1.1") Then 

            'I want the program to go to the first do loop now and check 2.0, 2.1 etc 



           End If 
          Loop 


         End If 
        Loop 

       End If 

      Loop 
     End If 
    Loop 





    Text File 

1.0 
Section 1 Title: 
1.1 
Section Title 
1.1.1 
Section Title 
1.1.1.1 
Section Title 
+1

這與[你的最後一個問題](http://stackoverflow.com/q/40897137/1070452)有什麼不同? – Plutonix

+0

@Plutonix ...我們不能將它作爲一個dup關閉。 – LarsTech

+0

它不同,因爲我加入了1.1.1.1。一旦程序讀取1.1.1.1我怎樣才能把它交給我的第一個循環。從不回答 – codeMonger123

回答

1

我會做這樣的事情假設文件看起來像它看起來像所有的時間:沒有多餘的註釋行,1條數據,並按順序排列:

Dim ver As New System.Version 

Dim data = File.ReadAllLines("C:\Temp\SockpuppetData.txt") 
Dim verData As New Dictionary(Of Version, String) 

Dim ndx As Int32 = 0 
Do Until ndx >= data.Count - 1 
    If String.IsNullOrEmpty(data(ndx)) = False Then 
     If System.Version.TryParse(data(ndx), ver) Then 
      ndx += 1 
      verData.Add(ver, data(ndx)) 
     End If 
    End If 
    ndx += 1 
Loop 

' debug 
For Each kvp In verData 
    Console.WriteLine("v: {0} t:{1}", kvp.Key, kvp.Value) 
Next 

最後,您將獲得一個版本對象字典和相關文本。如果有多行文字,請使用Dictionary(Of Version, String())。如果它們不合適,請使用List(Of KeyValuePair(of Version, String)),然後對其進行分類。

然後,它只是一個粘在一起的問題。結果:

ν:1.0噸:第1標題:
ν:1.1噸:章節標題
ν:1.1.1噸:章節標題
ν:1.1.1.1噸:章節標題
ν:2.0噸:章節名稱:
ν:2.1噸:章節標題
ν:2.1.1噸:章節標題
ν:2.1.1.1噸:章節標題

大鼠她比嘗試從一個循環/步驟開始到結束,將數據收集到您可以使用的東西:排序,計數,比較等。Version類型將允許您比較和確定事情。然後從中創建你的輸出。

樣本數據似乎有點均勻。例如,沒有什麼像1.2這會弄亂標題。在1.x和2.x之間的集合也是相同的,這使得這些標題偶然排隊。如果他們在列表中,則可以計算深度以知道要追加多少個Subs