2016-11-30 105 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