【发布时间】:2016-11-12 14:30:29
【问题描述】:
我想知道是否有更快的方法来“搜索”一行文本字符串的一部分并找到非静态值并将它们保存到变量中?
例如,我不能真正使用 Substring 来搜索行的一部分,因为 " " 中的值永远不会是相同的长度。
我正在阅读的文本文件的示例部分:
<I_Sect IDCode="20001" Description="This is desc" Quantity="1000" InclKind="Inc" />
id 名称:IDCode Description Quantity and InclKind 永不改变
值确实发生了变化:20001 ... This is desc... etc
在我使用子字符串查找 id 名称并获取“”之间的字符串有多长之后,是否有更快的方法来搜索“”?
当前代码:
Dim list As New List(Of String)()
Dim file As New System.IO.StreamReader(DisplayFile)
While Not file.EndOfStream
Dim line As String = file.ReadLine()
list.Add(line)
End While
file.Close()
Console.WriteLine("{0} lines read", list.Count)
'RichTextBox1.Text = System.IO.File.ReadAllText(DisplayFile)
For counter As Integer = 0 To list.Count
If list(counter).Substring(0, 7) = "<I_Sect" Then
'Do a substring of the line to see if I can locate Description ID string
' .............
Dim desc As String = ' .... [the valve I grab will be "This is desc"]
End If
Next
【问题讨论】:
-
整个 xml 文件是什么样的(显示所有可能节点的最小示例)?
-
xml 文件是半长的,但我只看这一行。因此,当我的代码逐行读取并从
-
很公平,但我总是建议在读取 xml 时使用 xml 序列化,这需要了解整个架构 - 或者至少是您感兴趣的子集。
-
我明白了。我当前的 Schema 非常标准 [也就是我必须搜索和获取所有 ID 值的一个主要元素] 我现在知道我可以尝试找到一个更“结构化”的子字符串搜索,但我将分配 IDCode、描述等作为新变量。所以我需要一个不那么“结构化”的子字符串检查,所以我可以获得所有 ID 值,因为这些值不是静态长度@Verdolino
-
到目前为止我在这一点上:
Dim startCheck = list(counter).Trim If startCheck.Contains("IDCode") Then Dim IDindex As Integer = startCheck.IndexOf("IDCode") If IDindex >= 0 Then Dim IDCode As String = startCheck.Substring(IDindex + 7 ....... End If End If但是,我正在尝试找到一种方法来获得介于“”@Verdolino 之间的这些值
标签: xml vb.net substring streamreader