vb.net 从文本向列表框添加多个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17901492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding multiple strings to a listbox from text
提问by NigeC
I have a text file I need to get multiple strings from, I can sort of do it but it only shows the first string in the listbox
我有一个文本文件,我需要从中获取多个字符串,我可以这样做,但它只显示列表框中的第一个字符串
When we tried with XML reader it was collecting everything in the XML that was tagged Object Identifier along with the cameras
当我们尝试使用 XML 阅读器时,它会收集 XML 中标记为对象标识符的所有内容以及相机
I need to look for lines similar to the following, there could be any amount
我需要寻找类似于以下的行,可能有任何数量
Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
key identifiers:
关键标识符:
./Cameras/
./相机/
Label="Standard Camera"
标签=“标准相机”
Type="Camera"
类型=“相机”
I could use "MyCamera" after ./Cameras/ or Name="MyCamera" both of these are common in each occurrence of the lines
我可以在 ./Cameras/ 或 Name="MyCamera" 之后使用“MyCamera”,这两者在每次出现的行中都很常见
in my example below it has the file I wish to read it should list 3 cameras https://www.dropbox.com/s/dy7r2auf9vv0m7g/testvb.zip
在我下面的示例中,它包含我希望阅读的文件,它应该列出 3 个摄像头 https://www.dropbox.com/s/dy7r2auf9vv0m7g/testvb.zip
The XML is generated by Thea render, its the scene file with the model, lights etc taken out so it just leaves cameras and some core settings
XML 是由 Thea 渲染生成的,它是带有模型、灯光等的场景文件,所以它只留下相机和一些核心设置
Thanks to varocarbas, this is the code that solves my problem:
感谢varocabas,这是解决我的问题的代码:
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
回答by varocarbas
You can use the XMLReaderI proposed in another answerand do the following modifications on it:
您可以使用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
This code retrieves all the information you want and stores it in LisBox1by putting " - " to separate each element. This is information more than enough and you should be the one performing any further change, for example: converting "./Cameras/MyCamera" into "./Cameras/" (there is an indication of how to do that in my previous code); or change the way the different items are displayed in the listBox(or perhaps you want to include one listBox per element: one for identifiers, another for labels, etc.).
此代码检索您想要的所有信息,并LisBox1通过放置“-”来分隔每个元素来将其存储。这些信息已经足够了,您应该是执行任何进一步更改的人,例如:将“./Cameras/MyCamera”转换为“./Cameras/”(在我之前的代码中指示了如何执行此操作) ; 或更改不同项目在 中的显示方式listBox(或者您可能希望为每个元素包含一个列表框:一个用于标识符,另一个用于标签等)。

