VBA中的SelectSingleNode失败

问题描述 投票:0回答:1

我正在尝试在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查询都没有返回任何内容。我在这个主题上看到的其他问题表明上述查询是正确的;我也许错过了别的什么?

excel xml vba msxml6
1个回答
0
投票
Quotes = oXMLDoc.SelectSingleNode("//a:feed")

应该

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

该错误消息具有误导性,因为它看起来似乎没有找到xpath目标,但如果发生这种情况它不会引发错误,它只会返回Nothing

© www.soinside.com 2019 - 2024. All rights reserved.