VB抓取文本值并保存为变量

时间:2016-11-12 14:30:29

标签: xml vb.net substring streamreader

我想知道是否有更快捷的方法来“搜索”一行文本的一部分,找到非静态值并将它们保存到变量中?

例如,我不能真正使用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

1 个答案:

答案 0 :(得分:1)

使用此xml文件

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <I_SecMain>
    <I_SecTop>
      <I_SecBrac>
        <I_Sect IDCode="20001" Description="This is desc 1" Quantity="10001" InclKind="Inc 1" />
        <I_Sect IDCode="20002" Description="This is desc 2" Quantity="10002" InclKind="Inc 2" />
        <I_Sect IDCode="20003" Description="This is desc 3" Quantity="10003" InclKind="Inc 3" />
        <I_Sect IDCode="20004" Description="This is desc 4" Quantity="10004" InclKind="Inc 4" />
        <I_Sect IDCode="20005" Description="This is desc 5" Quantity="10005" InclKind="Inc 5" />
      </I_SecBrac>
    </I_SecTop>
  </I_SecMain>
</root>

您可以定义可用于反序列化文件的相应模型。

导入的命名空间

Imports System.Xml.Serialization

模型

<XmlRoot("root")>
Public Class Root
    <XmlElement>
    Public Property I_SecMain As I_SecMain
End Class

Public Class I_SecMain
    <XmlElement>
    Public Property I_SecTop As I_SecTop
End Class

Public Class I_SecTop
    <XmlElement>
    Public Property I_SecBrac As I_SecBrac
End Class

Public Class I_SecBrac
    <XmlElement("I_Sect")>
    Public Property I_Sects As List(Of I_Sect)
End Class

Public Class I_Sect
    <XmlAttribute>
    Public Property IDCode As Integer
    <XmlAttribute>
    Public Property Description As String
    <XmlAttribute>
    Public Property Quantity As Integer
    <XmlAttribute>
    Public Property InclKind As String
End Class

非常简单地将文件反序列化为强类型对象

Dim DisplayFile = "test.xml"

Dim myRoot As Root
Dim mySerializer As New XmlSerializer(GetType(Root))
Using fs As New FileStream(DisplayFile, FileMode.Open)
    myRoot = mySerializer.Deserialize(fs)
End Using

可以迭代。

For Each isect In myRoot.I_SecMain.I_SecTop.I_SecBrac.I_Sects
    Console.WriteLine(
        String.Format("ID Code: {0}, Description: {1}, Quantity: {2}, InclKind: {3}",
                      isect.IDCode, isect.Description, isect.Quantity, isect.InclKind))
Next

从这里开始,这是一个准确定义模型的问题(你没有在你的问题中发布它),只是从反序列化的对象中检索属性。

使用序列化,如果需要,也可以轻松写入文件。

Using fs As New FileStream(DisplayFile, FileMode.Open)
    mySerializer.Serialize(fs, myRoot)
End Using