Pergunta

I have an XmlDataSource and a GridView on my page. On the Page_Load event, I apply an XPath to filter the xml elements according to the input of the user, LexiqueXmlDataSource.XPath = 'Some_XPath_here'; and it works just ok.

What I want is to access the elements that the XmlDataSource returns from codebehind after applying the XPath expression (and hence get their number).

I tried the GetXmlDocument() method but it returns the whole original Xml file rather than the filtered elements with XPath.

EDIT:

here is some code and the scenario I want:

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
}

Thank you.

Foi útil?

Solução

Here is what I could come up with.

For the number of hits. Use the GridView row count. Indeed the GetXmlDocument.ChildNodes.Count always return the number of lexical items, not the number of hits whan the XPath expression is applied.

Test XML

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

Minimalist ASP.net Page

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

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

Outras dicas

If I understand correctly you want to know the number of returned elements. Would the XPath expression 'count(Some_XPath_here)' not give this number of hits ?

here is some code and the scenario I want:

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
}

Just use and evaluate this XPath expression:

string xpath2 = "count(/lexique/item[starts-with(@acronym, '" + filter + "')])";
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top