Question

J'ai un XmlDataSource et un GridView sur ma page.Sur l'événement Page_Load, j'applique un XPath pour filtrer les éléments xml en fonction de la saisie de l'utilisateur, LexiqueXmlDataSource.XPath = 'Some_XPath_here'; et ça marche très bien.

Ce que je veux, c'est accéder aux éléments que XmlDataSource renvoie du codebehind après avoir appliqué l'expression XPath (et donc obtenir leur numéro).

j'ai essayé le GetXmlDocument() méthode mais elle renvoie l'intégralité du fichier XML d'origine plutôt que les éléments filtrés avec XPath.

MODIFIER:

voici du code et le scénario que je veux :

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
}

Merci.

Était-ce utile?

La solution

Voici ce que je pourrais proposer.

Pour le nombre de coups.Utilisez le nombre de lignes GridView.En effet, GetXmlDocument.ChildNodes.Count renvoie toujours le nombre d'éléments lexicaux, et non le nombre d'accès lorsque l'expression XPath est appliquée.

Tester XML

<?xml version="1.0" encoding="utf-8" ?>
<lexique>
    <item acronym="WPF" value="Windows Presentation Foundation"/>
    <item acronym="SO" value="StackOverflow"/>
</lexique>

Page ASP.net minimaliste

<%@ 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>

Code derrière

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

Autres conseils

Si je comprends bien, vous souhaitez connaître le nombre d'éléments renvoyés.L'expression XPath 'count(Some_XPath_here)' ne donnerait-elle pas ce nombre de hits ?

Voici du code et du scénario que je veux:

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
}

Utilisez et évaluez simplement cette expression XPath:

string xpath2 = "count(/lexique/item[starts-with(@acronym, '" + filter + "')])";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top