在Excel中将XML阅读到Treeview中

dzjeubhm  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(240)

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

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

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

  1. Private Sub LoadTreeViewFromXmlFile(ByVal file_name As String, ByVal trv As TreeView)
  2. Dim xml_doc As DOMDocument60
  3. ' Load the XML file into the DOMDocument.
  4. Set xml_doc = New DOMDocument60
  5. xml_doc.Load file_name
  6. ' Add the root node's children to the TreeView.
  7. TreeView1.Nodes.Clear
  8. AddChildrenToTreeView trv, Nothing, _
  9. xml_doc.DocumentElement
  10. End Sub
  11. ' Add this XML node's children to the indicated TreeView
  12. ' node.
  13. Private Sub AddChildrenToTreeView(ByVal trv As TreeView, ByVal treeview_parent As Node, ByVal xml_node As IXMLDOMElement)
  14. Dim xml_child As IXMLDOMElement
  15. Dim new_node As Node
  16. ' Examine each XML child. Error 13 Type mismatch - why?
  17. For Each xml_child In xml_node.ChildNodes
  18. ' Add the child to the TreeView.
  19. If treeview_parent Is Nothing Then
  20. Set new_node = trv.Nodes.Add(, , , _
  21. xml_child.nodeName)
  22. Else
  23. Set new_node = trv.Nodes.Add(treeview_parent, _
  24. tvwChild, , xml_child.nodeName)
  25. End If
  26. new_node.EnsureVisible
  27. ' Add the child's children.
  28. AddChildrenToTreeView trv, new_node, xml_child
  29. Next xml_child
  30. End Sub

2eafrhcq

2eafrhcq1#

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

相关问题