在Visual Basic中解析XML

时间:2015-05-20 19:45:45

标签: xml vb.net visual-studio-2013

我很难弄清楚如何在Visual Basic中“正确”解析XML响应。我碰巧运行的是Visual Studio 2013.我从查询的API中获得的响应看起来就像这样(一些数据发生了变化以保护无辜者):

<?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>

我可以解析它,但我这样做的方式肯定不直观。我花了几天的时间阅读它,我所能找到的只是看起来像我正在做的那样丑陋。显然我缺少一些基本的东西,我想知道如何阅读,这样我就可以学到正确的方法。

这是我正在使用的代码/流程。首先,我通过以下函数调用API来获取响应:

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

下面是我用来调用上面函数和解析的代码:

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

基本上我从响应中创建一个XML文档,然后搜索我感兴趣的元素,然后循环遍历该元素,寻找我感兴趣的子节点。

没有办法加载文档并以类似于:

的形式引用特定元素
doc.Elem("person").Elem("hos-status").Elem("availability").Elem("Shift").InnerText

我知道那里有什么元素(或应该存在),但无论我尝试什么,似乎无法直接引用它们。

2 个答案:

答案 0 :(得分:3)

你应该能够使用这样的东西:

doc.DocumentElement.Item("hos-status").Item("availability").Item("shift").InnerText

然而,关于VB.NET的一个好处是它使用System.Xml.Linq类(例如XElement)对XML的原生支持。这将允许你做这样的事情,这对我来说更具可读性(你需要弄清楚如何集成到你的代码中):

Dim xml = XElement.Load(wResponse.GetResponseStream())
Dim shift = x.<hos-status>.<availability>.<shift>.Value

答案 1 :(得分:1)

Dim xDoc as XMLDocument
xDoc.Load("C:\temp\file.xml")

Dim s as string = xDoc.SelectSingleNode("person/hos-status/availability/Shift").InnerText