XML 上的 GetSingleNode,命名空间不起作用
GetSingleNode on XML with namespace not working
我需要使用 TFS 中的全局列表。我已经下载了它,并且可以访问具有命名空间的 xml。我创建了 XmlNamespaceManager
,但我在获取所需节点时仍然遇到问题。对于那些不熟悉的人,TFS 全局列表如下所示:
<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" />
将全局列表下载到 XmlDocument
后,我使用以下代码尝试提取测试节点,但返回的是空值。
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)
附带说明一下,我正在搜索的名称确实存在。我也立即尝试了以下 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
解决方案
除了第一个节点之外,我不得不取出名称空间引用,并添加 @ 符号以访问下面答案中建议的属性。
Dim node As XmlNode = globalList.SelectSingleNode("//gl:GLOBALLISTS/GLOBALLIST[@name='Client Name']/LISTITEM[@value='" + Name + "']", nsManager)
在 XPath 中,属性使用 @
表示。这似乎是您遇到问题的原因,因为您没有使用 @
.
此外,正如您自己发现的那样,只有最外层的元素在命名空间中,因此只有第一段应该具有 gl:
.
Dim node As XmlNode = globalList.SelectSingleNode("/gl:GLOBALLISTS/GLOBALLIST[@name='Client Name']/LISTITEM[@value='" + Name + "']", nsManager)
我需要使用 TFS 中的全局列表。我已经下载了它,并且可以访问具有命名空间的 xml。我创建了 XmlNamespaceManager
,但我在获取所需节点时仍然遇到问题。对于那些不熟悉的人,TFS 全局列表如下所示:
<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" />
将全局列表下载到 XmlDocument
后,我使用以下代码尝试提取测试节点,但返回的是空值。
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)
附带说明一下,我正在搜索的名称确实存在。我也立即尝试了以下 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
解决方案
除了第一个节点之外,我不得不取出名称空间引用,并添加 @ 符号以访问下面答案中建议的属性。
Dim node As XmlNode = globalList.SelectSingleNode("//gl:GLOBALLISTS/GLOBALLIST[@name='Client Name']/LISTITEM[@value='" + Name + "']", nsManager)
在 XPath 中,属性使用 @
表示。这似乎是您遇到问题的原因,因为您没有使用 @
.
此外,正如您自己发现的那样,只有最外层的元素在命名空间中,因此只有第一段应该具有 gl:
.
Dim node As XmlNode = globalList.SelectSingleNode("/gl:GLOBALLISTS/GLOBALLIST[@name='Client Name']/LISTITEM[@value='" + Name + "']", nsManager)