我有一个文本文件,我需要从中获取多个字符串,我可以做到,但它只显示列表框中的第一个字符串
当我们尝试使用 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