在VB中从Web解析XML数据

时间:2012-01-11 00:32:55

标签: xml vb.net

我试图从网上解析数据,基本上是从API(Google Finance API :: http://www.google.com/ig/api?stock=AAPL)解析数据。

我该如何处理?我可以使用XMLDocument来执行此操作或MSXML2吗?

XML看起来像这样

<xml_api_reply version="1">
  <finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
     <symbol data="AAPL"/>
  </finance>
</xml_api_reply>

我只在节点根不具备我需要的名称的地方工作。那么,我将如何专门检索节点符号的数据?

这就是我目前所得到的:

Function get_ftseindex()
    Dim objXML As New System.Xml.XmlDocument
    objXML.Load("http://www.google.com/ig/api?stock=UKX")
End Function

1 个答案:

答案 0 :(得分:1)

您应该可以使用XDocument:

Public Sub Test()
    Dim sAPIUrl As String = "http://www.google.com/ig/api?stock=AAPL"
    Dim oDocument As XDocument = XDocument.Load(sAPIUrl)
    Dim sCompany As String = GetData(oDocument, "company")
    Dim sExchange As String = GetData(oDocument, "exchange")
    Dim dLast As Double = CDbl(GetData(oDocument, "last"))
    Dim dHigh As Double = CDbl(GetData(oDocument, "high"))
    Dim dLow As Double = CDbl(GetData(oDocument, "low"))
End Sub

Private Function GetData(ByVal doc As XDocument, ByVal name As String) As String
    Return doc.Root.Element("finance").Element(name).Attribute("data").Value
End Function