【问题标题】:VB grab text values and save as variablesVB抓取文本值并保存为变量
【发布时间】: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 &gt;= 0 Then Dim IDCode As String = startCheck.Substring(IDindex + 7 ....... End If End If 但是,我正在尝试找到一种方法来获得介于“”@Verdolino 之间的这些值

标签: xml vb.net substring streamreader


【解决方案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

【讨论】:

  • 只是出于好奇,您将 部分放在 vb 文件中的什么位置?它在将其传输到“定义类的页面顶部”时标记为未定义。 部分也一样
  • 那是模型定义。它需要位于您的代码可以访问它的地方,并且模型的类必须是公共的。你也应该Imports System.Xml.Serialization
  • 当模型的类名拼写与xml架构不完全一样时,可以传递名称。我这样做了两次;当根是“根”但我想大写该类(取决于您的架构)并且当我有一个列表时我想复数但元素具有单数名称。
  • 如果我正在阅读您的行 mySerializer.Deseriaize ... 它正在阅读文本文件。我已经用我以前的代码做到了这一点,所以无论如何要阅读我在没有该行的情况下给出的行并执行String.Format 部分?我问是因为mySerializer.Deserialize 给出了There is an error in XML document (2, 2). 的错误
  • 还有`Imports System.Xml.Serialization`修复了我之前的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多