XmlDocument.SelectNodes() Selects Nothing!!!!!!!

I came across what at first appeared to be an extremely strange problem. Pretty much I’m wanting to consume an MSBuild project file and perform a search on it for all the children of <ItemGroup>. For some reason the System.Xml.XmlDocument object I had created did not seem to be searching over the XML tree properly when I was running a SelectNodes() query. I have now found out its due to the fact that an XML namespace has been defined in the document element of the XML file and as a result I need to explicitly specify an XmlNamespaceManager. The following code extract solves the problem for both a situation where there is a xmlns attribute defined and for when there isn’t.

XmlDocument doc = new XmlDocument();
doc.LoadXml(SqlScriptXml);

XmlNodeList nodeList;

if (doc.DocumentElement.Attributes["xmlns"] != null)
{
    string xmlns = doc.DocumentElement.Attributes["xmlns"].Value;
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

    nsmgr.AddNamespace("MsBuild", xmlns);

    nodeList = doc.SelectNodes("/MsBuild:Project/MsBuild:ItemGroup/*", nsmgr);
}
else
{
    nodeList = doc.SelectNodes("/Project/ItemGroup/*");
}

foreach (XmlNode node in nodeList)
{
    string name = node.Name;
}

It’s obviously to do with the fact that as a xmlns attribute has been defined on the document element, when the System.Xml.XmlDocument object consumes the XML, all the tags used are assigned to be part of this namespace. As a result you need to explicitly specify a tag for that namespace and make use of the namespace tag when giving your XPath query to the SelectNodes() method.

18 thoughts on “XmlDocument.SelectNodes() Selects Nothing!!!!!!!

  1. You saved my day. I had exactly the same problem with the xmlns attribute. Thank you so much!! My blood pressure just returned to normal!!

  2. Thanks man!
    I’ve been Braking my head about this for at least 15 minutes before i’ve realized there must be a solution on the internet.
    You saved me from waisting hours on this!

  3. Thanks Chris for explaining this so well. After hours of research, I stumbled on to this article and all was fixed in my Excel file. Why can’t the MSDN have a simple level for busy developers.

  4. Thank you so very much. Finding your article helped me identify the same exact problem I encounter. It does look like a comment has been added MSDN as of today (8 years after your original post). It was your info that led me to understand why I was not getting any results from SelectNodes().

    https://msdn.microsoft.com/en-us/library/System.Xml.XmlNode.SelectNodes(v=vs.110).aspx

    Note
    If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get any nodes selected.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s