Search code examples
xmlxml-namespacesxmldocument

GetSingleNode on XML with namespace not working


I need to work with the Global List from TFS. I've downloaded it, and have access to the xml, which has a namespace. I created a XmlNamespaceManager, but I am still having issues getting the node that I need. For those not familiar, the TFS Global List looks like this:

<gl:GLOBALLISTS xmlns:gl="http://schemas.microsoft.com/VisualStudio/2005/workitemtracking/globallists">
    <GLOBALLIST name="Builds">
        <LISTITEM value="..." />
    </GLOBALLIST>
...
    <GLOBALLIST name="Client Name">
        <LISTITEM value="Test" />

After downloading the Global List into an XmlDocument, I use the following code to try to extract a test node, but I am getting a null back.

Dim globalList As XmlDocument = store.ExportGlobalLists()
Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(globalList.NameTable)
nsManager.AddNamespace("gl", "http://schemas.microsoft.com/VisualStudio/2005/workitemtracking/globallists")
Dim node As XmlNode = globalList.SelectSingleNode("//gl:GLOBALLISTS/gl:GLOBALLIST[@name='Client Name']/gl:LISTITEM[@value='" + Name + "']", nsManager)

As a side note, the name I am searching for does exist. I have also tried the following in the immediate window:

globalList.SelectNodes("/gl:GLOBALLISTS", nsManager)
Expression has been evaluated and has no value
globalList.SelectSingleNode("//gl:GLOBALLISTS/gl:GLOBALLIST[name='Client Name']/gl:LISTITEM[value='" + Name + "']", nsManager)
Expression has been evaluated and has no value
globalList.SelectSingleNode("//gl:GLOBALLIST[name='Client Name']/gl:LISTITEM[value='" + Name + "']", nsManager)
Expression has been evaluated and has no value
globalList.SelectSingleNode("//gl:GLOBALLIST[name='Client Name']", nsManager)
Expression has been evaluated and has no value
globalList.SelectSingleNode("//gl:GLOBALLIST", nsManager)
Expression has been evaluated and has no value
globalList.SelectSingleNode("//gl:GLOBALLISTS/GLOBALLIST[name='Client Name']/LISTITEM[value='Test']", nsManager)
Expression has been evaluated and has no value
globalList.SelectSingleNode("/gl:GLOBALLISTS/gl:GLOBALLIST[@name='Client Name']/gl:LISTITEM[@value='" + Name + "']", nsManager)
Expression has been evaluated and has no value

SOLUTION

I had to take out the namespace references except for the first node, and add the @ sign for accessing the attributes as suggested in the answer below.

Dim node As XmlNode = globalList.SelectSingleNode("//gl:GLOBALLISTS/GLOBALLIST[@name='Client Name']/LISTITEM[@value='" + Name + "']", nsManager)

Solution

  • In XPath, attributes are indicated using @. That seems to be the reason you're having a problem, since you're not using @.

    Also, as you've discovered on your own, only the outermost element is in a namespace, so only the first segment should have gl:.

    Dim node As XmlNode = globalList.SelectSingleNode("/gl:GLOBALLISTS/GLOBALLIST[@name='Client Name']/LISTITEM[@value='" + Name + "']", nsManager)