开发者

How can I parse XML data using Visual Basic

开发者 https://www.devze.com 2022-12-10 03:23 出处:网络
I have XML data like this: <search ver=\"3.0\"> <loc>Birmingham, AL</loc> <loc>Gulf Shores, AL</loc>

I have XML data like this:

<search ver="3.0">
  <loc>Birmingham, AL</loc> 
  <loc>Gulf Shores, AL</loc> 
  <loc>Alabama, NY</loc> 
  <loc>Abbeville, AL</loc> 
  <loc>Aber开发者_开发技巧nant, AL</loc> 
</search>

I would like to return the name of the places. How may I extract this data using Visual Basic?


Not sure why you don't do this in your XML, but maybe you have a good reason:

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

But anyway, if you get the loc elements in your original XML, you can use the Split method to pull it apart.


I got it:


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


// Easy way:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消