在 StreamReader 循环中强制读取下一行的任何方法?

Any way to force read next line while in StreamReader loop?

我正在处理一个 txt 文件,我想知道是否有一种方法可以在我使用它当前读取的值后强制流读取器读取下一行。

我的代码:

Private Sub subTest()
    Dim sr As New StreamReader("d:\input\cleanedUp.txt")
    Dim doc As New XmlDocument
    Dim root As XmlElement = doc.CreateElement("Views")
    Dim ns As String = sr.ReadLine()
    Dim nsAtt As XmlAttribute = doc.CreateAttribute("ClientCode")
    nsAtt.Value = ns
    root.Attributes.Append(nsAtt)

    While Not sr.EndOfStream
        Dim sLine As String = sr.ReadLine

        doc.AppendChild(root)
        Dim child As XmlElement = doc.CreateElement("View")
        root.AppendChild(child)
        Dim newAtt As XmlAttribute = doc.CreateAttribute("Name")
        newAtt.Value = sLine
        child.Attributes.Append(newAtt)

        Dim subChild As XmlElement = doc.CreateElement("Criteria")
        child.AppendChild(subChild)
        subChild.InnerText = sLine
    End While
    sr.Close() : sr.Dispose()

    Dim sett As New XmlWriterSettings
    sett.Indent = True

    Dim sw As XmlWriter = XmlWriter.Create("d:\input\data.xml", sett)
    doc.Save(sw)
    sw.Flush() : sw.Close()
End Sub

结果是这样的:

<?xml version="1.0" encoding="utf-8"?>
<Views ClientCode="WSMCABS">
    <View Name="vACCESSONE">
        <Criteria>vACCESSONE</Criteria>
    </View>
    <View Name="ACCESSONE">
        <Criteria>ACCESSONE</Criteria>
    </View>
</Views>

我要的是这个:

    <?xml version="1.0" encoding="utf-8"?>
<Views ClientCode="WSMCABS">
    <View Name="vACCESSONE">
        <Criteria>ACCESSONE</Criteria>
    </View>
    <View Name="vSample2">
        <Criteria>SAMPLE2</Criteria>
    </View>
</Views>

您可以测试循环中是否有另一行可用并执行另一个 ReadLine:

While Not sr.EndOfStream
    Dim sLine As String = sr.ReadLine
    DoSomethingWithLine(sLine)

    If Not sr.EndOfStream Then
        sLine = sr.ReadLine
        DoSomethingElseWithLine(sLine)
    End If
End While

所以我遇到的问题几乎是 reader 会为读取的每一行创建一个新的 child/subchild,而我想要一个有多个子子项的子项。所以这有点 hack,但我能够通过设置适用于我的情况的特定条件的循环来完成此操作。对于其他试图完成类似事情的人来说,这可能不是最好的答案,但它对我有用,因为我的文本文件由 SQL 视图组成,视图以 "v" 开头,后跟标准那个特定的观点。当程序看到在线上读取了另一个视图(以 "v" 开头)时,程序会中断添加其他子节点。

While Not sr.EndOfStream

        doc.AppendChild(root)
        Dim child As XmlElement = doc.CreateElement("View")
        root.AppendChild(child)
        Dim newAtt As XmlAttribute = doc.CreateAttribute("Name")
        newAtt.Value = sLine
        child.Attributes.Append(newAtt)

        If sLine.StartsWith("v") Then
            sLine = sr.ReadLine
            While Not sLine.StartsWith("v")
                Dim subChild As XmlElement = doc.CreateElement("Criteria")
                child.AppendChild(subChild)
                subChild.InnerText = sLine
                sLine = sr.ReadLine
                If sLine = Nothing Then
                    sLine = sr.ReadLine
                End If
                If sLine = Nothing Then
                    Exit While
                End If
            End While
        End If

    End While
    sr.Close() : sr.Dispose()