在Excel中将XML阅读到Treeview中

dzjeubhm  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(131)

我在Excel中有一个带有Treeview控件的小表单。引用包括OLE自动化,Microsoft Forms 2.0,Microsoft Windows Common Controls 6.0(SP6)和Microsoft XML,v6.0我调用LoadTreeViewFromXmlFile,在UserForm_Initialize()上提供有效的路径和文件名,如:

Call LoadTreeViewFromXmlFile("C:\Users\...\my.xml", TreeView1)

字符串
但我得到一个错误13类型不匹配,在子AddChildrenToTreeView和更具体地在For Each循环.不能理解为什么.有人能告诉我请?非常感谢你提前..

Private Sub LoadTreeViewFromXmlFile(ByVal file_name As String, ByVal trv As TreeView)

    Dim xml_doc As DOMDocument60

    ' Load the XML file into the DOMDocument.
    Set xml_doc = New DOMDocument60
    xml_doc.Load file_name

    ' Add the root node's children to the TreeView.
    TreeView1.Nodes.Clear
    AddChildrenToTreeView trv, Nothing, _
        xml_doc.DocumentElement

End Sub

' Add this XML node's children to the indicated TreeView
' node.
Private Sub AddChildrenToTreeView(ByVal trv As TreeView, ByVal treeview_parent As Node, ByVal xml_node As IXMLDOMElement)
    
    Dim xml_child As IXMLDOMElement
    Dim new_node As Node
    
    ' Examine each XML child. Error 13 Type mismatch - why?
    For Each xml_child In xml_node.ChildNodes
        ' Add the child to the TreeView.
        If treeview_parent Is Nothing Then
            Set new_node = trv.Nodes.Add(, , , _
                xml_child.nodeName)
        Else
            Set new_node = trv.Nodes.Add(treeview_parent, _
                tvwChild, , xml_child.nodeName)
        End If
        new_node.EnsureVisible

        ' Add the child's children.
        AddChildrenToTreeView trv, new_node, xml_child
    Next xml_child
    
End Sub

2eafrhcq

2eafrhcq1#

不是所有的子节点都是IXMLDOMElement类型-你也有文本节点,注解等。
这是针对.Net的,但你可以看到基本的想法:
https://learn.microsoft.com/en-us/dotnet/standard/data/xml/types-of-xml-nodes
您需要将xml_child声明为不太特定的类型(可能是IXMLDOMNode

相关问题