VBA中的SelectSingleNode故障

时间:2019-03-06 17:17:58

标签: excel xml vba msxml6

我正在尝试在VBA for Excel中解析返回的数据服务文件,尽管我已审查了类似问题的几个有用答案,但似乎没有一个能让SelectSingleNode为我返回任何东西。

XML文件很大,但是包装器节点如下:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://___.bpmonline.com/0/ServiceModel/EntityDataService.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <title type="text">labQuoteCollection</title>
    ...
</feed>

我的相关VBA代码在这里(我想知道其中某些属性是多余的,只是想确定一下):

Dim NS As String
NS = "xmlns:a=""http://www.w3.org/2005/Atom"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"""
Dim QuoteData As String
Dim oXMLReq As New WinHttp.WinHttpRequest
Dim oXMLDoc As New MSXML2.DOMDocument60
With oXMLHTTP
    .Open "GET", DataURL, False
    .setRequestHeader "Cookie", AuthCookie
    .send

    QuoteData = .responseXML.XML
    oXMLDoc.LoadXML QuoteData
    oXMLDoc.setProperty "SelectionLanguage", "XPath"
    oXMLDoc.setProperty "SelectionNamespaces", NS
    oXMLDoc.resolveExternals = True
    Debug.Print (oXMLDoc.parseError.ErrorCode)
    Debug.Print oXMLDoc.XML
    Dim Quotes As IXMLDOMNode
    Quotes = oXMLDoc.SelectSingleNode("//a:feed")
    If Quotes Is Nothing Then
        Debug.Print "fail"
    End If
End With

我可以看到XML毫无错误地加载到了oXMLDoc中,并且Print语句完整地输出了它,但是我尝试过的XPath查询都没有返回任何内容。我在该主题上看到的其他问题表明上述查询正确无误。我可能还缺少其他东西吗?

1 个答案:

答案 0 :(得分:0)

myproject

应该是

Quotes = oXMLDoc.SelectSingleNode("//a:feed")

该错误消息具有误导性,因为它似乎表明未找到xpath目标,但是如果发生这种情况,则不会引发错误,它只会返回Set Quotes = oXMLDoc.SelectSingleNode("//a:feed")