I just spent a few hours banging my head against a XPath query that didn't return the node I was trying to select using the right xpath string: to make a long story short, if your XML document has a default namespace without any prefix, to make XPath queries on it you have to use a NamespageManager, add the default namespace with a fake prefix, and then query the document specifying the namespace manager in the SelectNodes method.

But let's explain a bit more in detail. Imagine you want to make a XPath query on a VisualStudio project (the following is just a part of a real csproj file):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="2.0" DefaultTargets="Build"
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="Microsoft.Practices.EnterpriseLibrary.Common">
    <Reference Include="Microsoft.Practices.EnterpriseLibrary.Data">

If the xml document didn't have the default namespace


we could have queried the document to select the node with the reference for the Enterprise Library Data dll with the following XPath query string:

XmlNode entlibDataNode = doc.SelectSingleNode(

But unfortunately our xml document has a default namespace, so this query doesn't work: in order to make it work we need to instruct the XPath query engine to use the default namespace as follows:

XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);

The code above creates a XmlNamespaceManager and then add the document namespace with the fictitious prefix "a".

Once the XmlNamespaceManager has been set up we can go and query the document, but this time adding "a:" prefix before every node name:

XmlNode entlibDataNode = doc.SelectSingleNode(

I also noticed that this approach is probably used also by the XML Notepad 2007. If you open a VisualStudio project file with the XML Notepad and open the Find dialog selecting "Use XPath", and then you select a node of the XML, in the search string you will notice that it generates the XPath string using the prefix "a" before every node name.


Not sure if it's a bug or a feature, but this way it works. Hope this will save a bit of your time.

kick it on DotNetKicks.com

Technorati Tag: ,,