You are currently offline, waiting for your internet to reconnect

How To Use XPath Queries in MSXML DOM selectNodes Method

XML Path Language (XPath) queries can be used to query the XML documents with DOM methods such as selectNodes or selectSingleNode. The default query that is used is XSLPattern for backward compatibility. To use XPath, change the SelectionLanguage internal property of DOMDocument to XPath. XPath adds lot of functionality; for example, it allows you to use functions such as string-length and sum.
More information
The following code sample demonstrates how to use XPath with the selectNodes method:
  1. Start Visual Basic and create a new Standard EXE.
  2. On the menu, select Projects, select References, and then add a reference to Microsoft XML, v3.0.
  3. Add the following code to your Form_Load event:
    Dim dom As DOMDocument30Dim nodelist As IXMLDOMNodeListDim strPath As String   Set dom = New DOMDocument30dom.async = False   dom.loadXML "<Admin><Area AreaName='a'/></Admin>"   dom.setProperty "SelectionLanguage", "XPath"strPath = "/Admin/Area[string-length(@AreaName) = 1]"Set nodelist = dom.documentElement.selectNodes(strPath)   Debug.Print "Found " & nodelist.length & " Node"					
  4. Run the application, and note that the Immediate window shows Found 1 Node.
  5. To show the default behavior, comment out the code line that calls setProperty. Running the code then produces an error message because XSL pattern-matching does not support the string-length function.
  • With MSXML version 2.6, you need to make a reference to Microsoft XML, v2.6 in the Visual Basic project, and then use the corresponding ProgID of DOMDocument26.
  • If a newer version of MSXML has been installed in Side-by-Side mode, then to run the sample code with that specific version, you must explicitly use the GUIDs or ProgIDs for that version. For example, MSXML version 4 only installs in side-by-side mode. Please refer to the following article in the Microsoft Knowledge Base to see what code changes required to run the sample code with MSXML 4.0 parser: Q305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds. That is, with MSXML version 4.0, make a reference to Microsoft XML, v4.0 in the Visual Basic project, and then use the corresponding ProgID of DOMDocument40.
  • When programming with Microsoft Visual C++, the setProperty method is only available with IXMLDOMDocument2 interface.
  • For simplicity, the previous code does not include error checking. It is always a good practice to catch and handle errors.
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
317663 How To Access XML Data Using DOM in .NET Framework with Visual Basic .NET

Article ID: 288913 - Last Review: 06/22/2014 11:03:00 - Revision: 2.0

  • kbhowto KB288913