Reputation: 243
I am trying to use xpath and strip xml for the first time I,
all I want to do is get the first node to show in the debug window here is my code.
' 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
thanks for any help anyone can give!!!
here is the start of the 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
Upvotes: 3
Views: 2802
Reputation: 43743
First, you need to call the select method on the document, not on the null node variable:
'This will not work because node is null (Nothing)
node.SelectNodes("/situation")
'This will work
document.SelectNodes("/situation")
The SelectNodes
method returns a collection of nodes. If all you want is the first one, call SelectSingleNodes
instead, like this:
node = document.SelectSingleNode("/situation")
Then, rather than calling ToString
on the node, call either InnerXml
, InnerText
, or OuterXml
, depending on your preference, for instance:
node = document.SelectSingleNode("/situation")
If node IsNot Nothing Then
Debug.Print(node.InnerText)
Else
Debug.Print("Node does not exist")
End If
However, after looking at the actual XML that you are trying to read, that will never find the node. /situation
will only find the node if it is the root element, but in the actual XML document, it is here: /d2LogicalModel/payloadPublication/situation
. However, there is also a second problem. There is a default namespace defined on the root element: xmlns="http://datex2.eu/schema/1_0/1_0"
. Therefore, you need to explicitly specify the namespace in your select, like this:
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
Note that there is no need to create the HttpWebRequest
since the XmlDocument
class is capable of loading directly from a URI.
Upvotes: 1
Reputation: 149
Try this with SelectSingleNode Function.
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..
Upvotes: 1