Parsing XML in Visual Basic
I am having a hard time figuring out how to parse the XML response "correctly" in Visual Basic. I am running Visual Studio 2013. The response I get from the API I am requesting looks exactly the same (some data has been changed to protect the innocent):
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person id="123456">
<hos-status as-of="2015-05-20T18:53:39.592Z">
<duty-status>ON</duty-status>
<availability>
<drive>22020000</drive>
<shift>22020000</shift>
<cycle>151320000</cycle>
</availability>
<rest-break>
<max-drive>420000</max-drive>
<rest-remaining>1800000</rest-remaining>
</rest-break>
<daily-log-summary date="2015-05-20">
<duty-status code="OF">25200000</duty-status>
<duty-status code="SL">0</duty-status>
<duty-status code="DR">7800000</duty-status>
<duty-status code="ON">20580000</duty-status>
</daily-log-summary>
<daily-log-summary date="2015-05-19">
<duty-status code="OF">51600000</duty-status>
<duty-status code="SL">0</duty-status>
<duty-status code="DR">8580000</duty-status>
<duty-status code="ON">26220000</duty-status>
</daily-log-summary>
</hos-status>
<location as-of="2015-05-20T18:52:40.000Z">
<position lat="12.345678" lon="-12.345678" accuracy="3"/>
<speed>12</speed>
<bearing>123.45</bearing>
</location>
</person>
I can make it out, but the way I do it is of course not intuitive. I've spent a few days reading and all I can find are ways that look as ugly as what I am doing. Obviously I am missing something fundamental, and I would like you to talk about what to read so that I can learn the right path.
Here is the code / process I am using. First, I call the API to get the response via the following function:
Public Function getCurlXML(theURL As String) As Xml.XmlDocument
getCurlXML = New Xml.XmlDocument
Dim wHeader As WebHeaderCollection = New WebHeaderCollection()
wHeader.Clear()
Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(theURL), HttpWebRequest)
wRequest.ContentType = "text/xml"
wRequest.Headers = wHeader
wRequest.Method = "GET"
Dim wResponse As HttpWebResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)
Dim sResponse As String = ""
Using srRead As New StreamReader(wResponse.GetResponseStream())
sResponse = srRead.ReadToEnd()
End Using
getCurlXML.LoadXml(sResponse)
End Function
Below is the code I am using to call the above function and parse it:
Public Function getThePersonStatus(personID As String) As Collection
Dim statusColl As New Collection
Dim sUrl As String = "https://api.THE.com/api/stuffage/" & personID & "/status?apiKey=" & My.Settings.TheAPIKey
Dim doc As Xml.XmlDocument = getCurlXML(sUrl)
Dim statusElemList As XmlNodeList = doc.GetElementsByTagName("hos-status")
For Each thisNode As XmlNode In statusElemList
If (thisNode.Name = "hos-status") Then
statusColl.Add(thisNode.Attributes("as-of").Value.ToString, "hos-as-of")
statusColl.Add(thisNode("duty-status").InnerText.ToString, "duty-status")
Dim availElem As XmlNode = thisNode("availability")
statusColl.Add(availElem("drive").InnerText.ToString, "drive")
statusColl.Add(availElem("shift").InnerText.ToString, "shift")
statusColl.Add(availElem("cycle").InnerText.ToString, "cycle")
End If
Next
Dim locationElemList As XmlNodeList = doc.GetElementsByTagName("location")
For Each thisNode As XmlNode In locationElemList
statusColl.Add(thisNode.Attributes("as-of").Value.ToString, "loc-as-of")
statusColl.Add(thisNode("speed").InnerText.ToString, "speed")
statusColl.Add(thisNode("bearing").InnerText.ToString, "bearing")
statusColl.Add(thisNode("position").Attributes("lat").Value.ToString, "lat")
statusColl.Add(thisNode("position").Attributes("lon").Value.ToString, "lon")
statusColl.Add(thisNode("position").Attributes("accuracy").Value.ToString, "gps-accuracy")
Next
getThePersonStatus = statusColl
End Function
Basically I create an XML document from a response, then I look for the element I'm interested in, and then I loop through that element looking for the nodes I'm interested in.
It is not possible to load a document and refer to a specific element in a form like:
doc.Elem("person").Elem("hos-status").Elem("availability").Elem("Shift").InnerText
I know what elements exist (or should be there) but cannot refer to them directly no matter what I try.
source to share
You should be able to use something like this:
doc.DocumentElement.Item("hos-status").Item("availability").Item("shift").InnerText
However, one of the nice things about VB.NET is the built-in XML support using classes System.Xml.Linq
like XElement
. This will allow you to do something like this, which is much readable to me (you will need to figure out how to integrate into your code):
Dim xml = XElement.Load(wResponse.GetResponseStream()) Dim shift = x.<hos-status>.<availability>.<shift>.Value
source to share