【问题标题】:using xpath to retrieve one node from a xml file使用 xpath 从 xml 文件中检索一个节点
【发布时间】:2013-10-03 21:24:53
【问题描述】:

我第一次尝试使用 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文件的开始

 <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

【问题讨论】:

    标签: xml vb.net xpath


    【解决方案1】:

    首先,您需要在文档上调用 select 方法,而不是在空节点变量上:

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

    SelectNodes 方法返回一个节点集合。如果您只想要第一个,请致电SelectSingleNodes,如下所示:

    node = document.SelectSingleNode("/situation")
    

    然后,根据您的偏好,调用InnerXmlInnerTextOuterXml,而不是在节点上调用ToString,例如:

    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 加载。

    【讨论】:

    • 收到错误消息对象引用未设置为对象的实例。
    • 代码看起来像 Dim node As System.Xml.XmlNode node = document.SelectSingleNode("/Situation") Debug.Print(node.InnerText)
    • Debug.Print 行是否失败?如果是这样,那是因为 XML 文档中不存在 /Situation 元素。在打印值之前,您应该检查If node IsNot Nothing。这样,如果节点不存在,就不会引发异常。我更新了我的示例来演示如何做到这一点。
    • 是的,这就是失败的地方,如果节点,我为什么要选择一个复杂的布局来学习!!!!!!
    • 有没有一种简单的方法来调试。打印文档以便我可以仔细检查内容?我的硬拷贝显示第一行的情况?
    【解决方案2】:

    用 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..

    【讨论】:

    • ozoid?它看起来更像三角形,如果有的话...... :)
    猜你喜欢
    • 1970-01-01
    • 2011-05-30
    • 2013-07-19
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多