Microsoft Knowledge Base Email Alertz

XML Path Language (XPath) queries can be used to query the XML documents with DOM methods such as

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 288913 - Last Review: July 15, 2004 - Revision: 1.4

How To Use XPath Queries in MSXML DOM selectNodes Method

This article was previously published under Q288913

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.

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 = "http://support.microsoft.com/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.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
317663  (http://kbalertz.com/Feedback.aspx?kbNumber=317663/EN-US/ ) How To Access XML Data Using DOM in .NET Framework with Visual Basic .NET

APPLIES TO
  • Microsoft XML Parser 2.6
  • Microsoft XML Parser 3.0
  • Microsoft XML Core Services 4.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
Keywords: 
kbhowto KB288913
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate