这是我能想到的。
对于点击次数。使用 GridView 行数。事实上,GetXmlDocument.ChildNodes.Count 总是返回词汇项的数量,而不是应用 XPath 表达式时的命中数。
测试 XML
<?xml version="1.0" encoding="utf-8" ?>
<lexique>
<item acronym="WPF" value="Windows Presentation Foundation"/>
<item acronym="SO" value="StackOverflow"/>
</lexique>
极简 ASP.net 页面
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="xmlgrid.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="FilterLabel" runat="server" Text="Acronym starting with:"></asp:Label>
<asp:TextBox ID="FilterTextBox" runat="server"></asp:TextBox>
<asp:XmlDataSource ID="LexiqueXmlDataSource" runat="server" DataFile="~/lexique.xml">
</asp:XmlDataSource>
<asp:GridView ID="LexiqueGrid" runat="server" AllowSorting="true" BorderStyle="Groove">
<Columns>
<asp:TemplateField HeaderText="Acronym">
<ItemTemplate>
<%# XPath("/lexique/item/@acronym")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<%# XPath("/lexique/item/@value")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Hits" runat="server" Text="Acronyms found"></asp:Label>
<asp:Button ID="Submit" runat="server" Text="Search" />
</div>
</form>
</body>
</html>
代码背后
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim XPath As String
If String.IsNullOrEmpty(FilterTextBox.Text) Then
XPath = "/lexique/item"
Else
XPath = "/lexique/item[starts-with(@acronym, '" + FilterTextBox.Text + "')]"
End If
LexiqueXmlDataSource.XPath = XPath
LexiqueXmlDataSource.DataBind()
LexiqueGrid.DataSource = LexiqueXmlDataSource
LexiqueGrid.DataBind()
Hits.Text = "Selected " & LexiqueGrid.Rows.Count & " out of " &
LexiqueXmlDataSource.GetXmlDocument.ChildNodes.Count & "Acronyms loaded."
End Sub
End Class