Not able to get data from XML file in VBA?

前端 未结 2 1773
孤独总比滥情好
孤独总比滥情好 2021-01-27 02:30

I am trying to get data from XML file but can\'t get. I don\'t know what I am missing here.

XML File:



        
相关标签:
2条回答
  • 2021-01-27 02:58

    Try xml linq

    Imports System.Xml
    Imports System.Xml.Linq
    Module Module1
        Const FILENAME As String = "c:\temp\test.xml"
        Sub Main()
            Dim doc As XDocument = XDocument.Load(FILENAME)
    
            Dim results = doc.Descendants("TestObject").Select(Function(x) New With { _
                .site = CType(x.Element("Site"), String), _
                .name = CType(x.Element("Name"), String), _
                .url = CType(x.Element("URL"), String) _
            }).ToList()
        End Sub
    
    End Module
    
    0 讨论(0)
  • 2021-01-27 03:10

    Since Sites, Namesand URLs are Object, you need to use theSet keyword.

    Set Sites = oXMLFile.SelectNodes("TestClass/TestObject/Site/text()")
    Set Names = oXMLFile.SelectNodes("TestClass/TestObject/Name/text()")
    Set URLs = oXMLFile.SelectNodes("TestClass/TestObject/URL/text()")
    

    I'm surprised you didn't get an error, but it may be because Object are specials.

    Since SelectNodes returns a IXMLDOMNodeList, you might want to replace your definitions by:

    Dim oXMLFile as DOMDocument
    Dim Sites As IXMLDOMNodeList
    Dim Names As IXMLDOMNodeList
    Dim URLs As IXMLDOMNodeList
    
    0 讨论(0)
提交回复
热议问题