2013-09-27 59 views
3

我試圖使用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開始

回答

1

首先,你需要調用文檔上的選擇方法,而不是空節點變量:

'This will not work because node is null (Nothing) 
node.SelectNodes("/situation") 

'This will work 
document.SelectNodes("/situation") 

SelectNodes方法返回的集合節點。如果你想要的是第一種,叫SelectSingleNodes代替,就像這樣:

node = document.SelectSingleNode("/situation") 

之後,而不是在節點上調用ToString,打電話要麼InnerXmlInnerText,或OuterXml,根據自己的喜好,比如:

node = document.SelectSingleNode("/situation") 
If node IsNot Nothing Then 
    Debug.Print(node.InnerText) 
Else 
    Debug.Print("Node does not exist") 
End If 

但是,在查看您嘗試讀取的實際XML後,將永遠找不到節點。 /situation只會查找節點(如果它是根元素),但在實際的XML文檔中,它位於:/d2LogicalModel/payloadPublication/situation。但是,還有第二個問題。在根元素上定義了一個默認名稱空間:xmlns="http://datex2.eu/schema/1_0/1_0"。因此,你需要明確指定命名空間在你的選擇,像這樣:

Dim doc As New XmlDocument() 
doc.Load("http://hatrafficinfo.dft.gov.uk/feeds/datex/England/CurrentRoadworks/content.xml") 
Dim nsmgr As New XmlNamespaceManager(doc.NameTable) 
nsmgr.AddNamespace("x", "http://datex2.eu/schema/1_0/1_0") 
Dim node As XmlNode = doc.SelectSingleNode("/x:d2LogicalModel/x:payloadPublication/x:situation", nsmgr) 
If node IsNot Nothing Then 
    Debug.Print(node.InnerXml) 
Else 
    Debug.Print("Node does not exist") 
End If 

注意,沒有必要創建HttpWebRequest因爲XmlDocument類是能夠加載直接從URI。

+0

獲取錯誤消息對象引用未設置爲對象的實例。 – user2247671

+0

代碼看起來像Dim node As System.Xml.XmlNode node = document.SelectSingleNode(「/ Situation」) Debug.Print(node.InnerText) – user2247671

+0

它是否在'Debug.Print'行失敗?如果是這樣,那是因爲XML文檔中不存在'/ Situation'元素。打印值之前,你應該檢查'如果節點IsNot Nothing'。這樣,如果節點不存在,您不會導致異常。我更新了示例以演示如何做到這一點。 –

1

與功能的SelectSingleNode試試這個。

Dim xrn As XmlNode 
Dim xd As New XmlDocument() 
xd.LoadXml(xml) 
xrn = xd.SelectSingleNode("//") 

If Not IsNothing(xrn) Then 
    mac = xrn.InnerText 
End If 

ozoid ..

+0

ozoid?它看起來更加三角形,如果有的話... :) –