XML文件结构如下:<?xml version="1.0" encoding="utf-8"?>
<Database name="SQL Server" type="Standard Security">
<Provider>sqloledb</Provider>
<DataSource>(local)</DataSource>
<InitialCatalog>pubs</InitialCatalog>
<UserId>sa</UserId>
<Password>123456</Password>
</Database>

解决方案 »

  1.   

    我大概写法是这样的:
    Private root As IXMLDOMElement
    Private nodeList As IXMLDOMNodeList
    Private node As IXMLDOMNode    ……(省略)    Set root = xmlDocument.documentElement
        Set nodeList = root.selectNodes("Database")
        Set node = nodeList.Item(1)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~执行这步后,node的值是Nothing,应该就错在这里了    SelectNodeValue = node.Text
      

  2.   

    Private Sub Command1_Click()
    Set xmlDoc = CreateObject("Msxml2.DOMDocument")
    xmlDoc.async = False
    xmlDoc.validateOnParse = False
    If xmlDoc.Load("c:\ToWord.xml") = False Then
        Set parseError = xmlDoc.parseError
        MsgBox "Load xml failure." & vbCrLf & parseError.reason
        Exit Sub
    End IfSet nodeList = xmlDoc.documentElement.selectSingleNode("//Database").childNodes
    msgbox nodeList.Item(0).textEnd Sub
      

  3.   


    那如果不是指定Item的Index呢,而是选取特定的Item,如何实现?
      

  4.   


    唉,自己搞清楚了,原来selectSingleNode(nodeName)就是选出指定节点的。原来NodeList在VB里没什么太大意义,遍历的时候才有用……
      

  5.   

    Private Sub Command1_Click()
    Set xmlDoc = CreateObject("Msxml2.DOMDocument")
    xmlDoc.async = False
    xmlDoc.validateOnParse = False
    If xmlDoc.Load("c:\ToWord.xml") = False Then
        Set parseError = xmlDoc.parseError
        MsgBox "Load xml failure." & vbCrLf & parseError.reason
        Exit Sub
    End If
    xmlDoc.setProperty "SelectionLanguage", "XPath" 
    Set node= xmlDoc.documentElement.selectSingleNode("//Database/Provider")
    msgbox node.text
    End Sub