Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Use XPath Queries in MSXML DOM selectNodes Method


Summary

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.

↑ Back to the top


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 DOMDocument30
    Dim nodelist As IXMLDOMNodeList
    Dim strPath As String

    Set dom = New DOMDocument30
    dom.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.
NOTE:
  • 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.

↑ Back to the top


References

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

↑ Back to the top


Keywords: kbdsupport, kbhowto, kb

↑ Back to the top

Article Info
Article ID : 288913
Revision : 1
Created on : 1/7/2017
Published on : 6/22/2014
Exists online : False
Views : 347