-1

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

当我们尝试使用 XML 阅读器时,它会收集 XML 中标记为 Object Identifier 的所有内容以及相机

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

对象标识符="./Cameras/MyCamera" Label="标准相机" Name="MyCamera" Type="Camera"

关键标识符:

./相机/

标签="标准相机"

类型="相机"

我可以在 ./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
4

1 回答 1

1

您可以使用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(或者您可能希望每个元素包含一个列表框:一个用于标识符,另一个用于标签等)。

于 2013-07-27T20:02:10.807 回答