我試圖使用XPath和帶XML爲我第一次一個節點,使用XPath來從一個XML文件
所有我想要做的就是第一個節點在調試窗口,顯示這裏是我的代碼。
' Create a WebRequest to the remote site
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://hatrafficinfo.dft.gov.uk/feeds/datex/England/CurrentRoadworks/content.xml")
Dim response As System.Net.HttpWebResponse = request.GetResponse()
' Check if the response is OK (status code 200)
If response.StatusCode = System.Net.HttpStatusCode.OK Then
Dim stream As System.IO.Stream = response.GetResponseStream()
Dim reader As New System.IO.StreamReader(stream)
Dim contents As String = reader.ReadToEnd()
Dim document As New System.Xml.XmlDocument()
document.LoadXml(contents)
Dim node As System.Xml.XmlNode
For Each node In document
Debug.Print(node.SelectNodes("/situation").ToString())
Next node
Else
Throw New Exception("Could not retrieve document from the URL, response code: " & response.StatusCode)
End If
感謝任何人都可以給予的幫助!
這裏是XML doument
<d2LogicalModel modelBaseVersion="1.0">
<exchange>
<supplierIdentification>
<country>gb</country>
<nationalIdentifier>NTCC</nationalIdentifier>
</supplierIdentification>
</exchange><payloadPublication xsi:type="SituationPublication" lang="en"> <publicationTime>2013-09-27T16:09:02+01:00</publicationTime>
GB開始
獲取錯誤消息對象引用未設置爲對象的實例。 – user2247671
代碼看起來像Dim node As System.Xml.XmlNode node = document.SelectSingleNode(「/ Situation」) Debug.Print(node.InnerText) – user2247671
它是否在'Debug.Print'行失敗?如果是這樣,那是因爲XML文檔中不存在'/ Situation'元素。打印值之前,你應該檢查'如果節點IsNot Nothing'。這樣,如果節點不存在,您不會導致異常。我更新了示例以演示如何做到這一點。 –