从文本向列表框添加多个字符串

时间:2013-07-27 19:13:20

标签: vb.net visual-studio-2010 text listbox

我有一个文本文件,我需要从中获取多个字符串,我可以这样做,但它只显示列表框中的第一个字符串

当我们尝试使用XML阅读器时,它收集了XML中标记为对象标识符的所有内容以及相机

我需要查找类似于以下内容的行,可能有任何数量

Object Identifier =“./ Cameras / MyCamera”Label =“Standard Camera”Name =“MyCamera”Type =“Camera”

密钥标识符:

./相机/

Label =“标准相机”

类型= “摄像机”

我可以在./Cameras/或Name =“MyCamera”之后使用“MyCamera”这两种情况在每次出现的行中都很常见

在我下面的例子中,它有我希望阅读的文件应该列出3个摄像头 https://www.dropbox.com/s/dy7r2auf9vv0m7g/testvb.zip

XML是由Thea渲染生成的,它的场景文件包含模型,灯光等,因此它只是离开了摄像机和一些核心设置

感谢varocarbas,这是解决我问题的代码:

Dim path As String = "C:\Users\jen\Desktop\test\temp.xml"
    Dim settings As System.Xml.XmlReaderSettings = New System.Xml.XmlReaderSettings()
    settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment
    Using reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(path)
        While (reader.Read())
            If (reader.NodeType = System.Xml.XmlNodeType.Element) Then
                If (reader.Name = "Object") Then

                    'Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
                    Dim Identifier As String = reader.GetAttribute("Identifier") '"./Cameras/MyCamera"
                    Dim Label As String = reader.GetAttribute("Label") '"Standard Camera"
                    Dim Name As String = reader.GetAttribute("Name") '"MyCamera"
                    Dim Type As String = reader.GetAttribute("Type") '"Camera"

                    Dim wholeString As String = Name  'WHOLE STRING TO BE ADDED TO THE LISTBOX

                    'Adding the string to ListBox1
                    If (wholeString.Trim.Length > 0) And Type = "Camera" Then
                        ListBox1.Items.Add(wholeString)
                    End If


                End If
            End If
        End While
    End Using

1 个答案:

答案 0 :(得分:1)

您可以使用我在another answer中提出的XMLReader并对其进行以下修改:

Dim path As String = "temp.txt"
Dim settings As System.Xml.XmlReaderSettings = New System.Xml.XmlReaderSettings()
settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment
Using reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(path)
    While (reader.Read())
        If (reader.NodeType = System.Xml.XmlNodeType.Element) Then
            If (reader.Name = "Object") Then

                'Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
                Dim Identifier As String = reader.GetAttribute("Identifier") '"./Cameras/MyCamera"
                Dim Label As String = reader.GetAttribute("Label") '"Standard Camera"
                Dim Name As String = reader.GetAttribute("Name") '"MyCamera"
                Dim Type As String = reader.GetAttribute("Type") '"Camera"

                Dim wholeString As String = Identifier & " - " & Label & " - " & Name & " - " & Type 'WHOLE STRING TO BE ADDED TO THE LISTBOX

                'Adding the string to ListBox1
                If (wholeString.Trim.Length > 0) Then
                    ListBox1.Items.Add(wholeString)
                End If


            End If
        End If
    End While
End Using

此代码检索您想要的所有信息,并将其存储在LisBox1中,方法是将“ - ”分隔开来。这是足够的信息,您应该是执行任何进一步更改的人,例如:将“./Cameras/MyCamera”转换为“./Cameras/”(在我之前的代码中有指示如何执行此操作) ;或者更改listBox中不同项目的显示方式(或者您希望每个元素包含一个listBox:一个用于标识符,另一个用于标签等)。