2016-11-12 17 views
0

我想知道是否有更快的方法來「搜索」一行文本字符串的一部分,並找到非靜態值並將它們保存到變量?VB抓取文本值並保存爲變量

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

你的整個xml文件是什麼樣的(最小的例子顯示所有可能的節點)? – djv

+0

該XML文件是半長,但我只看着這一行。因此,當我的代碼逐行讀取並從 narue1992

+0

夠公平的,但我總是建議在讀取xml時使用xml序列化,並且需要知道整個模式 - 或者至少是您感興趣的子集。 – djv

回答

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 
+0

只是出於好奇,你把部分放在vb文件中?當它傳遞到」定義類的頁面頂部「時,它標誌着未定義的標誌, 。部分也一樣 – narue1992

+0

這是模型定義,它需要放在你的代碼可以訪問它的地方,模型的類必須是公共的,你也應該'Imports System.Xml.Serialization' – djv

+0

當模型的類名稱與拼寫不完全相同,您可以傳遞名稱。我做了兩次;當根是「根」的時候,但我想把這個類大寫(取決於你的模式),當我有一個列表時,我想要複數化,但元素有單數名稱。 – djv