Alex Rivera | Logout

Using Xpath With Default Namespace in C#

Asked 2009-02-25T12:34:57.783
67

I've got an XML document with a default namespace. I'm using a XPathNavigator to select a set of nodes using Xpath as follows:

XmlElement myXML = ...;  
XPathNavigator navigator = myXML.CreateNavigator();
XPathNodeIterator result = navigator.Select("/outerelement/innerelement");

I am not getting any results back: I'm assuming this is because I am not specifying the namespace. How can I include the namespace in my select?

Edit
Report

2 Answers

49

You might want to try an XPath Visualizer tool to help you through.

XPathVisualizer is free, easy to use.

alt text

IMPORTANT: If you are using Windows 7/8 and don't see File, Edit and Help Menu items, please press ALT key.

answered 2009-12-12T17:17:34.043
8

You can use XPath statement without using XmlNamespaceManager like this:

...
navigator.Select("//*[ local-name() = 'innerelement' and namespace-uri() = '' ]")
...

That is a simple way of selecting element within XML with default namespace definied.

The point is to use:

namespace-uri() = ''

which will found element with default namespace without using prefixes.

answered 2012-03-14T13:19:20.647

Your Answer