2

ページにXmlDataSourceとGridViewがあります。Page_Loadイベントで、XPathを適用して、ユーザーの入力に従ってxml要素をフィルター処理します。これは問題LexiqueXmlDataSource.XPath = 'Some_XPath_here';なく機能します。

私が欲しいのは、XPath式を適用した後にXmlDataSourceが分離コードから返す要素にアクセスすることです(したがって、それらの番号を取得します)。

このメソッドを試しましたGetXmlDocument()が、XPathでフィルター処理された要素ではなく、元のXmlファイル全体が返されます。

編集:

これが私が望むいくつかのコードとシナリオです:

protected void Page_Load(object sender, EventArgs e)
{

            string xpath = "/lexique/item[starts-with(@acronym, '" + filter + "')]";
            LexiqueXmlDataSource.XPath = xpath;

            // Here the XmlDataSource have filtered the xml elements to return to the GridView
            //I want to know how many element passed this filter using the XmlDataSource itself
}

ありがとうございました。

4

3 に答える 3

1

私が正しく理解していれば、返された要素の数を知りたいと思うでしょう。XPath式'count(Some_XPath_here)'は、このヒット数を提供しませんか?

于 2011-02-12T22:29:49.457 に答える
1

これが私が思いついたものです。

ヒット数について。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
于 2011-02-21T17:57:46.573 に答える
0

これが私が望むいくつかのコードとシナリオです:

protected void Page_Load(object sender, EventArgs e)
{

            string xpath = "/lexique/item[starts-with(@acronym, '" + filter + "')]";
            LexiqueXmlDataSource.XPath = xpath;

            // Here the XmlDataSource have filtered the xml elements to return to the GridView
            //I want to know how many element passed this filter using the XmlDataSource itself
}

このXPath式を使用して評価するだけです。

string xpath2 = "count(/lexique/item[starts-with(@acronym, '" + filter + "')])";
于 2011-02-19T17:08:57.270 に答える