Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   |   Class Browser
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

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.

C# XPathWithXmlDoc.exe
View Source

[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Xml\XPathWithXmlDoc\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]

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

		
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(document);
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 � 2005 Microsoft Corporation. All rights reserved.


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2008  Mindcracker LLC. All Rights Reserved