为什么这个 XPath 表达式获取太多数据?
我有一个 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>
I have a XPath expression that is supposed to return/get only one node out of the XML document. But it is getting more than the one. I don't understand why.
Code-behind:
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 Document:
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找不到上面的代码/XML 有什么问题。
运行您的示例按预期工作,
addingItr.Count
返回 1。我通过迭代返回的节点来仔细检查这一点,如下所示:
您正在运行什么代码来确定返回节点的计数?
编辑
来源:XPathNodeIterator 类< /a>
所以调试时看到的是根节点的 InnerXml 属性。
I cannot find anything wrong with the above code/XML.
Running your example works as expected, with
addingItr.Count
returning 1.I double checked this by iterating the returned nodes like so:
What code are you running to determine the count of the returned nodes?
Edit
From: XPathNodeIterator Class
So what you are seeing when debugging is the InnerXml property of the root node.