How to ignore a XML namespace

后端 未结 1 1775
盖世英雄少女心
盖世英雄少女心 2021-01-25 14:42

I have an XML file and this XML file has namespaces declared



        
相关标签:
1条回答
  • 2021-01-25 15:27

    This worked for me (after some amount of head-scratching)

    Sub Tester()
    
        Const XML As String = "<?xml version='1.0'?>" & _
        "<CrystalReport  xmlns='urn:crystal-reports:schemas:report-detail' " & _
        " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " & _
        " xsi:schemaLocation='urn:crystal-reports:schemas:report-detail " & _
        " http://www.businessobjects.com/products/xml/CR2008Schema.xsd'>" & _
        "   <Test>Testing</Test>" & _
        "</CrystalReport>"
    
        Dim xmlDom As New MSXML2.DOMDocument60
        Dim nodeList As MSXML2.IXMLDOMNodeList
        Dim iNode As MSXML2.IXMLDOMNode
    
        With xmlDom
            .async = False
            .validateOnParse = True
            .LoadXML XML
            .setProperty "SelectionLanguage", "XPath"
    
            'set the default namespace and give it a prefix (e.g.) "xx"
            .setProperty "SelectionNamespaces", _
                         "xmlns:xx='urn:crystal-reports:schemas:report-detail'"
    
            'use the same default prefix in your XPath
            Set nodeList = .SelectNodes("//xx:Test")
    
        End With
    
        Debug.Print nodeList.Length
        For Each iNode In nodeList
            Debug.Print iNode.XML
        Next iNode
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题