2011-02-12 73 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 + "')])";