Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

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
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Xml\XPathWithXmlDoc\]

The following code creates the XmlDocument object and loads the XML document.

		
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(args);
C#

The following code creates the XmlNamespaceManager object and maps namespaces to namespace prefixes used in the XPath queries.

		
XmlNamespaceManager nsmanager = 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");
C#

The following code selects XML data using an XPath query.

		
XmlNodeList nodelist = myXmlDocument.SelectNodes("//yourns1:book", nsmanager);
C#



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.