0
我有一個XPath表達式,應該返回/從XML文檔中只獲取一個節點。但它不僅僅是一個。我不明白爲什麼。爲什麼這個XPath表達式獲得太多數據?
代碼隱藏:
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>
在調試過程中,我將鼠標懸停在addsItr變量上並遍歷屬性。我看到這個屬性是「InnerXML」。 – dotnetN00b
似乎你是對的。我想我太早檢查了迭代器。本應該等到MoveNext。謝謝,jdavies。 – dotnetN00b