这是我试图解析的 XML 响应:
<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.webserviceX.NET/">0.7627</double>
VB代码:
Dim responseString As String
Dim fromCurrency As String = "EUR"
Dim toCurrency As String = "USD"
Dim req As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=" & fromCurrency & "&ToCurrency=" & toCurrency)
Dim Resp As System.Net.HttpWebResponse = req.GetResponse()
Dim reader As StreamReader = New StreamReader(Resp.GetResponseStream)
responseString = reader.ReadToEnd()
'get the XML
Dim objXML As New XmlDocument
objXML.LoadXml(responseString)
Dim root As XmlNode = objXML.DocumentElement
Dim nodeList As XmlNodeList = root.SelectNodes("/double")
如何读取“0.7627”的结果值?
我试过:
nodeList(0).InnerText
和
nodeList(0).SelectSingleNode("/double").InnerText
两者都抛出错误“对象引用未设置为对象的实例”。
我还尝试添加一个命名空间管理器:
Dim mgr As XmlNamespaceManager = New XmlNamespaceManager(objXML.NameTable)
mgr.AddNamespace("currency", objXML.DocumentElement.NamespaceURI)
Dim node As XmlNode = objXML.SelectSingleNode("double", mgr)
并通过以下方式选择:
node.InnerText
但这也会引发“对象引用未设置为对象的实例”。