2011-02-12 94 views
2

我的頁面上有一個XmlDataSource和一個GridView。在Page_Load事件中,我應用XPath根據用戶的輸入過濾xml元素,LexiqueXmlDataSource.XPath = 'Some_XPath_here';並且它工作正常。循環通過XmlDataSource中的XML元素後面的代碼

我想要的是訪問XmlDataSource在應用XPath表達式(從而得到它們的編號)後從代碼隱藏中返回的元素。

我試過GetXmlDocument()方法,但它返回的是整個原始的Xml文件,而不是使用XPath過濾的元素。

編輯:

這裏是一些代碼和情景我想:

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 
} 

謝謝。

+0

不`計數(your_expression_here)`工作? – Flack 2011-02-12 22:36:39

+0

我想從XmlDataSource訪問元素,而不是自己訪問xml文件 – 0xFF 2011-02-12 22:49:44

+0

我認爲還不清楚......您是否想訪問「Page_Load」事件中的選定節點,但是不需要再次查詢XML源?那麼你不需要XPath表達式。重新標記 – 2011-02-12 23:19:44

回答

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 
1

如果我理解正確,你想知道返回元素的數量。 XPath表達式「count(Some_XPath_here)」會不會給出這個命中數?

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 + "')])";