Vb.net如何只读取HTML模板文件的特定部分

问题描述

我有一个使用占位符的HTML模板文件,将在读取模板文件后将其替换。根据代码中的条件,我只想读取HTML模板文件的一部分。

例如,如果condition = False, 我只想准备模板文件中以占位符[IFVALUEFALSESection]开头的部分。

由于File.ReadAllText()-读取所有内容而不是特定部分,如何实现呢?

解决方法

您的代码必须实现您想要的逻辑,例如

Dim x as New List(Of String)
Dim capturingSection = False

For Each s In File.ReadLines(path)
  If s.Contains("[ENDIFVALUEFALSESection]" Then Exit For
  If capturingSection Then x.Add(s)
  If s.Contains("[IFVALUEFALSESection]" Then capturingSection = True
Next s

'now you have the lines of the section in x