【问题标题】:Parse XML data from the Web in VB在 VB 中解析来自 Web 的 XML 数据
【发布时间】:2012-01-11 00:32:55
【问题描述】:

我正在尝试解析来自网络的数据,基本上,来自 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

【问题讨论】:

    标签: xml vb.net


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多