我有一个关于从命名空间 xml 中检索节点的问题。只要我在查询中指定 xml 的单个元素,我就可以检索所有匹配的元素,并且可以遍历它们的子元素。当我尝试通过指定更详细的查询来检索单个唯一元素时,我的问题就出现了。
我用于测试的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<<MTConnectStreams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectStreams:1.2 http://mtconnect.org/schemas/MTConnectStreams_1.2.xsd" xmlns="urn:mtconnect.org:MTConnectStreams:1.2">
<Streams>
<Samples>
<Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="x_pos_cmd" subType="COMMANDED" units="MILLIMETER">1</Position>
<Position dataItemId="xs5" timestamp="2012-11-29T12:30:19Z" sequence="1718270" name="x_pos_act" subType="ACTUAL" units="MILLIMETER">2</Position>
<Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="y_pos_cmd" subType="COMMANDED" units="MILLIMETER">3</Position>
<Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="y_pos_act" subType="COMMANDED" units="MILLIMETER">4</Position>
<Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="z_pos_cmd" subType="COMMANDED" units="MILLIMETER">5</Position>
</Samples>
<Condition>
<Unavailable dataItemId="xc1" type="POSITION" timestamp="2012-11-27T23:19:40Z" sequence="7" name="x_cond_pos">UNAVAILABLE</Unavailable>
<Unavailable dataItemId="xc2" type="LOAD" timestamp="2012-11-27T23:19:40Z" sequence="8" name="x_cond_load">UNAVAILABLE</Unavailable>
</Condition>
</Streams>
</MTConnectStreams>
给定以下代码:
Dim xIt As XPathNodeIterator
Dim xDoc As XmlDocument = New XmlDocument
xDoc.Load("c:\Download\Test.xml")
Dim xNav As XPathNavigator = xDoc.CreateNavigator
Dim nsMgr = New XmlNamespaceManager(xNav.NameTable)
nsMgr.AddNamespace("ns", "urn:mtconnect.org:MTConnectStreams:1.2")
sQuery = "//ns:Samples"
xIt = xNav.Select(sQuery, nsMgr)
MsgBox(xIt.Count.ToString)
When sQuery = "//ns:Samples" (1 result)
When sQuery = "//ns:Position" (5 results)
When sQuery = "//ns:Samples/Position (0 results why?)
When sQuery = "//ns:Samples/Position[@name='x_pos_act'] (0 results)
When sQuery = "//ns:Position[@name='x_pos_act']" (0 results)
When sQuery = "//ns:Position/[@name='x_pos_act']" (0 results)
等等等等。每当查询不止一个节点名称时,我都没有得到任何结果。我无法弄清楚如何指定更详细的查询。