2

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.

0xFF
  • 4,140
  • 7
  • 41
  • 58
  • Doesn't `count(your_expression_here)` work? – Flack Feb 12 '11 at 22:36
  • I want to access the elements from the XmlDataSource, not access the xml file myself – 0xFF Feb 12 '11 at 22:49
  • I think is not clear... Do you want to access the selected nodes in the `Page_Load` event but without query the XML source again? Then you don't need an XPath expression. Retagging –  Feb 12 '11 at 23:19

3 Answers3

1

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 ?

Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
  • I don't want to apply the XPath myself, I want to query the XmlDataSource for the elements that it filtered using the XPath expression – 0xFF Feb 12 '11 at 22:33
  • So I understand the XPath expression is a kind of black box and you focus on the resulting node set (supposedly returned from – Alain Pannetier Feb 12 '11 at 23:14
  • @Alain Pannetier it's exactly the way you said, but when I query XmlDataDocument.ChildNodes.count it returns the number of nodes *in* the xml file and not the xml elements that the XmlDataSource returns when it apply the XPath expression – 0xFF Feb 13 '11 at 00:24
  • @Alain Pannetier the XPath expression works perfectly, because I can see the elements change in the GridView – 0xFF Feb 13 '11 at 00:25
  • Ooops, I had forgotten about your issue. Are you still interested ? – Alain Pannetier Feb 19 '11 at 19:03
  • Could you post some of the XML you are using somewhere (e.g. pastebin) and if possible some of the relevant code ? – Alain Pannetier Feb 21 '11 at 14:17
1

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
Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
  • That's what I call a solution :D thanx – 0xFF Feb 21 '11 at 20:30
  • Sorry but it seems you forgot to award the bounty ? – Alain Pannetier Feb 22 '11 at 06:33
  • OH really? I marked your answer so it must get the bounty automatically no? how do I do it otherwise? – 0xFF Feb 22 '11 at 16:36
  • Actually, the standard way is to do it manually. Otherwise there are some rules called automatic award. [here](http://meta.stackexchange.com/questions/16065/how-does-the-bounty-system-work) are the ins and outs about bounties. – Alain Pannetier Feb 22 '11 at 16:56
  • Oh, I'm very very sorry, actually the bounty is awarded automatically is the best answer has at least 2 votes! I didn't award it manually and it didn't get awarded automatically? so who's got the points? – 0xFF Feb 23 '11 at 18:16
  • You must still have them I guess ;-) – Alain Pannetier Feb 23 '11 at 19:25
  • Actually no, I just had a look at your reputation and the bounty was taken from you. – Alain Pannetier Feb 23 '11 at 20:08
  • OK. I understand. If you don't award the bounty and the auto rules are not satisfied (which only gives *half* the points), then the bounty is lost for everybody. See [here](http://meta.stackexchange.com/questions/55199/why-didnt-i-get-the-bounty) for the meta article. So don't worry, I learned something anyway. Next time ;-) – Alain Pannetier Feb 23 '11 at 20:15
0

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 + "')])";
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431