如何使用Visual Basic解析XML数据

时间:2009-11-01 05:32:18

标签: xml vb.net

我有这样的XML数据:

<search ver="3.0">
  <loc>Birmingham, AL</loc> 
  <loc>Gulf Shores, AL</loc> 
  <loc>Alabama, NY</loc> 
  <loc>Abbeville, AL</loc> 
  <loc>Abernant, AL</loc> 
</search>

我想返回这些地方的名字。如何使用Visual Basic提取此数据?

3 个答案:

答案 0 :(得分:0)

不确定为什么不在XML中执行此操作,但也许您有充分的理由:

<loc>
    <city>Birmingham</city>
    <state>AL</state>
</loc>

但无论如何,如果你在原始XML中获得loc元素,你可以使用Split method将它拉开。

答案 1 :(得分:0)

我明白了:


public function returnstring()as string() 
Dim suggestions As List(Of String) = New List(Of String)
Dim baseUrl As String = "path to my xml file"
settings.IgnoreWhitespace = True
settings.IgnoreComments = True

Using reader As XmlReader = XmlReader.Create(url, settings) While (reader.Read()) If (reader.NodeType = XmlNodeType.Element And reader.LocalName = "loc" ) Then

                    suggestions.Add(reader.ReadElementString("loc"))

                End If
            End While

End Using return suggestions.toarray()

end function

答案 2 :(得分:0)

//简单方法:

Dim docxml As New DOMDocument30
Dim element As IXMLDOMElement

Dim Node As IXMLDOMNode

docxml.Load app.path & "configiw.xml"
text1.text = docxml.getElementsByTagName("loc").Item(0).Text 
'Zero is the number of the line, you can use a while or a for