【问题标题】:Loop through XML elements in XmlDataSource in code behind在后面的代码中循环遍历 XmlDataSource 中的 XML 元素
【发布时间】:2011-02-12 22:21:09
【问题描述】:

我的页面上有一个 XmlDataSource 和一个 GridView。在 Page_Load 事件中,我应用 XPath 根据用户的输入过滤 xml 元素,LexiqueXmlDataSource.XPath = 'Some_XPath_here';,它工作正常。

我想要的是在应用 XPath 表达式后访问 XmlDataSource 从代码隐藏返回的元素(并因此获取它们的编号)。

我尝试了 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
}

谢谢。

【问题讨论】:

  • count(your_expression_here) 不工作吗?
  • 我想从 XmlDataSource 访问元素,而不是自己访问 xml 文件
  • 我认为不清楚...您是否要访问 Page_Load 事件中的选定节点但又不查询 XML 源?那么您就不需要 XPath 表达式了。重新标记

标签: xpath xmldatasource


【解决方案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

【讨论】:

  • 这就是我所说的解决方案:D thanx
  • 抱歉,您好像忘记奖励赏金了?
  • 哦,真的吗?我标记了你的答案,所以它必须自动获得赏金不是吗?否则我该怎么做?
  • 其实标准的做法是手动完成。否则有一些规则称为自动奖励。 here 是关于赏金的来龙去脉。
  • 哦,非常非常抱歉,实际上赏金是自动授予的,最佳答案至少有 2 票!我没有手动授予它,它没有自动授予?那么谁有积分呢?
【解决方案2】:

如果我理解正确,您想知道返回元素的数量。 XPath 表达式 'count(Some_XPath_here)' 不会给出这个命中数吗?

【讨论】:

  • 我不想自己应用 XPath,我想在 XmlDataSource 中查询它使用 XPath 表达式过滤的元素
  • 所以我理解 XPath 表达式是一种黑盒子,您专注于结果节点集(假设从您应用
  • @Alain Pannetier 这正是你所说的,但是当我查询 XmlDataDocument.ChildNodes.count 它返回节点的数量 in xml 文件而不是 xml 元素XmlDataSource 在应用 XPath 表达式时返回
  • @Alain Pannetier XPath 表达式完美运行,因为我可以看到 GridView 中的元素发生变化
  • 糟糕,我忘记了您的问题。你还有兴趣吗?
【解决方案3】:

这是一些代码和我的场景 想要:

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

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多