在XML文档中选择具有多个名称空间值的节点不返回任何内容

有一个xml文件,如:

   Demo   

我在做

 Dim m_xmld As XmlDocument m_xmld = New XmlDocument() m_xmld.Load("myXML.xml") Dim test As XmlNode test = doc.SelectSingleNode("Data/Info", GetNameSpaceManager(m_xmld)) 

有:

  Public Shared Function GetNameSpaceManager(ByRef xDoc As XmlDocument) As XmlNamespaceManager Dim nsm As New XmlNamespaceManager(xDoc.NameTable) Dim RootNode As XPathNavigator = xDoc.CreateNavigator() RootNode.MoveToFollowing(XPathNodeType.Element) Dim NameSpaces As IDictionary(Of String, String) = RootNode.GetNamespacesInScope(XmlNamespaceScope.All) For Each kvp As KeyValuePair(Of String, String) In NameSpaces nsm.AddNamespace(kvp.Key, kvp.Value) Next Return nsm End Function 

但是,在阅读xml时,我继续"Nothing" 。 有没有办法忽略命名空间? 问题是某些命名空间可能因文件而异,这就是为什么我添加了GetNameSpaceManager函数…

在XPath中,不带前缀的元素名称始终在空命名空间中考虑。 但是在XML中, 默认名称空间默认情况下隐式inheritance哪些元素,这个元素在您的特定XML中:

 xmlns="uuid:ebfd9-45-48-a9eb-42d" 

我建议在你的XPath中使用默认前缀,比如d 。 然后将前缀映射到根元素的命名空间:

 ...... Dim nsManager As New XmlNamespaceManager(New NameTable()) nsManager.AddNamespace("d", m_xmld.DocumentElement.NamespaceURI) test = doc.SelectSingleNode("d:Data/d:Info", nsManager) 

上述内容适用于两种情况(带有和不带默认命名空间的XML文档),但不适用于在后代元素级别本地声明的默认命名空间的XML。