我在下面有一个简单的 xml 文档:
<?xml version="1.0" encoding="utf-8"?>
<Research researchID="RT_a" language="eng" xmlns="http://www.rixml.org/2010/1/RIXML" createDateTime="2012-06-30T01:13:38Z">
<Product productID="RT_a">
<SecurityID idType="ISIN" idValue="US0605051046" />
</Product>
</Research>
我正在尝试使用给定的 XPath 字符串读取属性“idValue”。除非我删除这部分,否则这不起作用:
xmlns="http://www.rixml.org/2010/1/RIXML"
我的代码如下:
Dim doc As XPathDocument = New XPathDocument("c:\test\test.xml")
Dim strXPath As String = "/Research[1]/Product[1]/SecurityID[1]"
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim mgr As New XmlNamespaceManager(nav.NameTable)
mgr.AddNamespace("", "http://www.rixml.org/2010/1/RIXML")
Dim XPathNode As XPathNavigator = nav.SelectSingleNode(strXPath, mgr)
If XPathNode IsNot Nothing Then
Console.WriteLine(XPathNode.GetAttribute("idValue", String.Empty))
Else
Console.WriteLine("Nothing found")
End If
关于添加名称空间的行对结果没有影响 - 只是做了一些测试。我需要做什么/我做错了什么?