getElementsByTagName 在 VBA 中返回具有 DOMDOCUMENT60 的零元素

getElementsByTagName returning zero elements with DOMDOCUMENT60 in VBA

我有一段 VBA 代码可以在旧环境中完美运行,使用 MSXML2.DOMDOCUMENT 对象。但是,现在我不得不将它们变成 MSXML2.DOMDOCUMENT60 对象(因为 Excel 64 位不支持 MSXML2.DOMDOCUMENT)它不再有效。

具体来说,我为 getElementsByTagName 返回了零个节点(尽管我遇到了类似的问题,例如 selectNodesselectSingleNode。我怀疑名称空间(我已经阅读了无数不同的关于该主题的帖子),但我无法让它发挥作用。

请注意,所有数据都存在于文件中,如果我在 VBA 编辑器中使用 Locals window,那么我可以检查所有数据。只是拒绝被代码返回

这是我使用的代码:

' Open the TXC XML file
Dim TXCDoc As New msxml2.DOMDocument60
With TXCDoc
    .async = False
    .validateOnParse = True
    .setProperty "SelectionLanguage", "XPath"
    .setProperty "SelectionNamespaces", "xmlns=""http://www.transxchange.org.uk"""
    .Load filename
End With

If TXCDoc.parseError.ErrorCode <> 0 Then
    MsgBox "Parsing error in file " & filename & Chr$(10) & Chr$(10) & TXCDoc.parseError.reason & Chr$(10) & Chr$(10) & _
           "Line: " & TXCDoc.parseError.Line & ":" & TXCDoc.parseError.linepos, vbCritical + vbOKOnly, "TXC Validation Error"
End If
    
' Read the VehicleJourneys    
Dim xmlVjList As IXMLDOMNodeList
Dim xmlVj As IXMLDOMNode
Set xmlVjList = TXCDoc.getElementsByTagName("VehicleJourney")
For Each xmlVj In xmlVjList
    ' Do stuff
Next xmlVj

[编辑澄清 - xmVjList returns 零 VehicleJourney 个节点]

这是我尝试加载的 XML 文件的片段:

<?xml version="1.0" encoding="utf-8"?>
<TransXChange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.transxchange.org.uk/ http://www.transxchange.org.uk/schema/2.1/TransXChange_general.xsd" CreationDateTime="2020-07-21T09:57:00-00:00" ModificationDateTime="2020-07-21T09:57:00-00:00" Modification="new" RevisionNumber="0" FileName="SVRYEAG001.xml" SchemaVersion="2.1" RegistrationDocument="false" xmlns="http://www.transxchange.org.uk/">
  <StopPoints> ... </StopPoints>
  <RouteSections> ... </RouteSections>
  <Routes> ... </Routes>
  <JourneyPatternSections> ... </JourneyPatternSections>
  <Operators> ... </Operators>
  <Services> ... </Services>
  <VehicleJourneys>
    <VehicleJourney> ... </VehicleJourney>
    <VehicleJourney> ... </VehicleJourney>
    <VehicleJourney> ... </VehicleJourney>
    <VehicleJourney> ... </VehicleJourney>
    <!-- there are 22 VehicleJourney nodes in total -->
  </VehicleJourneys>
</TransXChange>

那我做错了什么?谢谢。

感谢@gserg,这是我现在使用的代码:

' Open the TXC XML file
Dim TXCDoc As New msxml2.DOMDocument60
With TXCDoc
    .async = False
    .validateOnParse = True
    .setProperty "SelectionLanguage", "XPath"
    .setProperty "SelectionNamespaces", "xmlns:txc=""http://www.transxchange.org.uk/"""
    .Load filename
End With

If TXCDoc.parseError.ErrorCode <> 0 Then
    MsgBox "Parsing error in file " & filename & Chr$(10) & Chr$(10) & TXCDoc.parseError.reason & Chr$(10) & Chr$(10) & _
       "Line: " & TXCDoc.parseError.Line & ":" & TXCDoc.parseError.linepos, vbCritical + vbOKOnly, "TXC Validation Error"
End If
    
' Read the VehicleJourneys    
Dim xmlVjList As IXMLDOMNodeList
Dim xmlVj As IXMLDOMNode
Set xmlVjList = TXCDoc.selectNodes("//txc:VehicleJourneys/txc:VehicleJourney")
For Each xmlVj In xmlVjList
    ' Do stuff
Next xmlVj