How Do I...Select XML Data Using XPath?
This sample illustrates how to select XML data in an XML document using the XPath selection methods
of the XmlDocument class. An XmlDocument object is used to load the XML document and to select XML data using
XPath queries. An XmlNamespaceManager object is used to map namespaces to namespace prefixes used in the XPath
queries. Finally, the SelectNodes method of the XmlDocument class is used to select XML data from the XML
document using XPath queries.
VB XPathWithXmlDoc.exe
The following code creates the XmlDocument object and loads the XML document.
Dim myXmlDocument As New XmlDocument()
myXmlDocument.Load(document)
VB
The following code creates the XmlNamespaceManager object and maps namespaces to namespace prefixes used in the XPath
queries.
Dim nsmanager As New XmlNamespaceManager(myXmlDocument.NameTable)
nsmanager.AddNamespace("ns", "http://tempuri.org/myBooksNamespace")
nsmanager.AddNamespace("myns", "http://tempuri.org/myBooksProcessornamespace")
nsmanager.AddNamespace("yourns1", "http://tempuri.org/myBook1namespace")
nsmanager.AddNamespace("yourns2", "http://tempuri.org/myBook2namespace")
VB
The following code selects XML data using an XPath query.
Dim nodelist As XmlNodeList = myXmlDocument.SelectNodes("//yourns1:book", nsmanager)
VB
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|