我有一个XPath表达式,它应该返回/只从XML文档中获取一个节点。但它正在变得越来越多。我不明白为什么。
代码隐藏:
Dim xmlNameTbl As XmlNameTable = rootDoc.NameTable
Dim xmlNS As XmlNamespaceManager = New XmlNamespaceManager(xmlNameTbl)
xmlNS.AddNamespace("asp", "http://test.com/asp")
Dim sectionPosition As String = rowNode.GetAttribute("ID")
'In this example sectionPosition is "A03"
Dim sectionLetter As String = rowNode.GetAttribute("ID").Substring(0, 1)
Dim sectionRowNumberText As String = rowNode.GetAttribute("ID").Remove(0, 1)
Dim sectionRowNumber As Integer
Integer.TryParse(sectionRowNumberText, sectionRowNumber)
Dim addingNav As XPathNavigator = rootDoc.CreateNavigator
Dim hello = rootDoc.ChildNodes
Dim addingItr As XPathNodeIterator = addingNav.Select("//asp:TableRow[@ID='" & sectionPosition & "']", xmlNS)
'Nodes with A03 and A02 are being returned, even though it should be only A03 returned
XML文档:
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:asp="http://test.com/asp" xmlns:meta="http://test.com/meta" xmlns:cc1="http://test.com/cc1">
<asp:TableRow ID="A03">
<asp:TableCell>
<asp:Localize ID="tagthreeCtrlNumberRes" meta:resourcekey="tagthreeCtrlNumberRes" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="tagthreeCtrlDescRes" meta:resourcekey="tagthreeCtrlDescRes" runat="server" />
<asp:Localize ID="tagthreeCtrlNoteRes" meta:resourcekey="tagthreeCtrlNoteRes" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:RadioButtonList ID="rblthreeCtrlRes0" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res0" Value="1" />
<asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res0" Value="0" />
<asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res0" Value="2" />
</asp:RadioButtonList>
<asp:RadioButtonList ID="rblthreeCtrlRes1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Text="Yes" meta:resourcekey="rblthreeCtrl0Res1" Value="1" />
<asp:ListItem Text="No" meta:resourcekey="rblthreeCtrl1Res1" Value="0" />
<asp:ListItem Text="N/A" meta:resourcekey="rblthreeCtrl2Res1" Value="2" />
</asp:RadioButtonList>
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="cmdthreeCtrlRes" meta:resourcekey="cmdthreeCtrlRes" runat="server" OnClick="FormDataSave_Click" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="lblAssmthreeCtrlRes" meta:resourcekey="lblAssmthreeCtrlRes" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="lblQualthreeCtrlRes" meta:resourcekey="lblQualthreeCtrlRes" runat="server" />
<asp:Button ID="cmdQualAcceptthreeCtrlRes" meta:resourcekey="cmdQualAcceptthreeCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True" />
</asp:TableCell>
<asp:TableCell />
<asp:TableCell />
</asp:TableRow>
<asp:TableRow ID="A04">
<asp:TableCell>
<asp:Localize ID="tagoneCtrlNumberRes" meta:resourcekey="tagoneCtrlNumberRes" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="tagoneCtrlDescRes" meta:resourcekey="tagoneCtrlDescRes" runat="server" />
<asp:Localize ID="tagoneCtrlNoteRes" meta:resourcekey="tagoneCtrlNoteRes" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="tagtxtoneCtrlRes0" meta:resourcekey="tagtxtoneCtrlRes0" runat="server" />
<asp:Textbox ID="txtoneCtrlRes0" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="cmdoneCtrlRes" meta:resourcekey="cmdoneCtrlRes" runat="server" OnClick="FormDataSave_Click" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="lblAssmoneCtrlRes" meta:resourcekey="lblAssmoneCtrlRes" runat="server" />
</asp:TableCell>
<asp:TableCell />
<asp:TableCell />
<asp:TableCell />
</asp:TableRow>
<asp:TableRow ID="A02">
<asp:TableCell>
<asp:Localize ID="tagtwoCtrlNumberRes" meta:resourcekey="tagtwoCtrlNumberRes" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="tagtwoCtrlDescRes" meta:resourcekey="tagtwoCtrlDescRes" runat="server" />
<asp:Localize ID="tagtwoCtrlNoteRes" meta:resourcekey="tagtwoCtrlNoteRes" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="tagtxttwoCtrlRes0" meta:resourcekey="tagtxttwoCtrlRes0" runat="server" />
<asp:Textbox ID="txttwoCtrlRes0" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="cmdtwoCtrlRes" meta:resourcekey="cmdtwoCtrlRes" runat="server" OnClick="FormDataSave_Click" />
</asp:TableCell>
<asp:TableCell>
<asp:Localize ID="lblAssmtwoCtrlRes" meta:resourcekey="lblAssmtwoCtrlRes" runat="server" />
</asp:TableCell>
<asp:TableCell />
<asp:TableCell />
<asp:TableCell />
</asp:TableRow>
</Root>
答案 0 :(得分:1)
我发现上述代码/ XML没有任何问题。
运行示例按预期工作,addingItr.Count
返回1。
我通过迭代返回的节点来仔细检查这个:
For Each n As XPathNavigator In addingItr
Dim nodeName As String = n.Name
Next
您运行了哪些代码来确定返回节点的数量?
修改强>
XPathNavigator类返回的XPathNodeIterator对象是 未定位在选定节点集中的第一个节点上。打电话给 必须使用XPathNodeIterator类的MoveNext方法 将XPathNodeIterator对象放在第一个节点上 选定的节点集。
所以你在调试时看到的是根节点的InnerXml属性。