Microsoft Knowledge Base Email Alertz

KBAlertz.com: When you use the

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]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

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




Article ID: 308062 - Last Review: July 14, 2004 - Revision: 2.2

How To Specify Fully Qualified Element Names in XPath Queries by Using Visual Basic .NET

This article was previously published under Q308062

On This Page

SUMMARY

When you use the XmlDocument object to load and parse an Extensible Markup Language (XML) document, it is common programming practice to identify elements with a specific element name. This article demonstrates how to specify fully qualified element names in the NamespacePrefix:ElementName format to select nodes in an XmlDocument.

Create the XML File

  1. On the Windows Start menu, point to Run, type notepad.exe, and then click OK to open Notepad.
  2. Copy and paste the following XML code into Notepad:
    <?xml version="1.0"?>
       <bk:Books xmlns:bk='http://myserver/myschemas/Books'>
         <bk:Book>
             <bk:Title>Just XML</bk:Title>
         </bk:Book>
         <bk:Book>
             <bk:Title>Professional XML</bk:Title>
         </bk:Book>
         <bk:Book>
             <bk:Title>XML Step by Step</bk:Title>
         </bk:Book>
         <bk:Book>
             <bk:Title>XML By Example</bk:Title>
         </bk:Book>
       </bk:Books>
    					
  3. On the File menu, click Save.
  4. In the Save As dialog box, in the Save as type text box, click All Files. In the File name text box, type Books.xml, and then click OK.

Create the Visual Basic .NET Project

The following code sample uses the following objects and classes:
  • The XPathNavigator class: XPathNavigator is based on the XML Path Language (XPath) data model and provides the methods that are required to implement XPath queries over any data store.
  • The XPathExpression class: This class encapsulates a compiled XPath expression and is returned when you call Compile. The Select, Evaluate, and Matches methods use this class.
  • The XmlNamespaceManager class: XmlNamespaceManager resolves, adds, and removes namespaces to a collection. XmlNamespaceManager also provides scope management for these namespaces. Because Books.xml uses the "bk" namespace in the code to follow, you must use XmlNamespaceManager.
  • The XPathNodeIterator class: This object provides an iterator over a set of selected nodes.
To create and run the Visual Basic .NET project, follow these steps:
  1. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
  2. Place a Button control and a TextBox control on Form1.
  3. Set the MultiLine property of the TextBox control to True.
  4. Click to expand the TextBox control so that you can view four or five lines of data.
  5. Add the following code to the top of the Code window:
    Imports System.Xml
    Imports System.Xml.XPath
    					
  6. To load the Books.xml file into an XmlDocument object, add the following code to the Button1_Click event:
    Dim oxmldoc As New XmlDocument()
    oxmldoc.Load("c:\Books.xml")
    					
  7. Make sure that the Books.xml path in the preceding code points to the correct path on your computer.
  8. Use the CreateNavigator method of the XmlDocument object to create the XPathNavigator object so that you can run the XPath query:
    Dim oXPathNav As XPathNavigator
    oXPathNav = oxmldoc.CreateNavigator
    					
  9. Use the Compile method of XPathNavigator to create an XPathExpression class, and then pass the XPath query as the parameter:
    Dim Expr As XPathExpression
    Expr = oXPathNav.Compile("//bk:Book[position()>=2]")
    					
  10. Use the the AddNamespace method to add the "bk" namespace to the XmlNamespaceManager object:
    Dim oxmlNSManager As New XmlNamespaceManager(oXPathNav.NameTable)
    oxmlNSManager.AddNamespace("bk", "http://myserver/myschemas/Books")
    					
  11. Use the SetContext method of XPathExpression to set the XPathExpression context to the XmlNamespaceManager:
    Expr.SetContext(oxmlNSManager)
    					
  12. To run the XPath query and return the selected nodes, pass the expression to the Select method of the XPathNodeIterator:
    Dim iterator As XPathNodeIterator = oXPathNav.Select(Expr)
    While iterator.MoveNext
        TextBox1.Text = TextBox1.Text + vbCrLf + iterator.Current.Value
    End While
    					
  13. The code in Button1_Click event should appear as follows:
    Dim oxmldoc As New XmlDocument()
            Try
                oxmldoc.Load("c:\Books.xml")
    
                Dim oXPathNav As XPathNavigator
                oXPathNav = oxmldoc.CreateNavigator
    
                Dim Expr As XPathExpression
                Expr = oXPathNav.Compile("//bk:Book[position()>=2]")
    
                Dim oxmlNSManager As New XmlNamespaceManager(oXPathNav.NameTable)
                oxmlNSManager.AddNamespace("bk", "http://myserver/myschemas/Books")
                Expr.SetContext(oxmlNSManager)
    
                Dim iterator As XPathNodeIterator = oXPathNav.Select(Expr)
                While iterator.MoveNext
                    TextBox1.Text = TextBox1.Text + vbCrLf + iterator.Current.Value
                End While
    
                oxmlNSManager = Nothing
                oXPathNav = Nothing
                oxmldoc = Nothing
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    					
  14. Build and run the project.
  15. Click Button1. Notice that a list of books whose position is greater than or equal to 2 appears in the textbox.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
280457  (http://kbalertz.com/Feedback.aspx?kbNumber=280457/EN-US/ ) PRB: Specifying Fully Qualified Element Names in XPath Queries

APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
Keywords: 
kbhowtomaster KB308062
       

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

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please