用于 Repeater 的 ItemDataBound 内的 Xpath

发布于 2024-08-12 17:37:48 字数 1091 浏览 5 评论 0原文

好的,我使用 XMLDataSource 和中继器从 feedburner 中提取 XML 提要。

<asp:Repeater ID="rptrEvents" OnItemDataBound="rptrEvents_ItemDataBound" DataSourceID="XmlDataSource1" runat="server">
    <ItemTemplate>
            <li runat="server" id="liLineItem">
                <a href="<%#XPath("link")%>">
                    <span><%#XPath("pubdate")%></span>
                    <%#XPath("title")%>
                </a>
            </li>
    </ItemTemplate>
</asp:Repeater>

现在我遇到的问题是,“pubdate”是空的,标题包括标题和日期[尽管它可以很容易地被分割,因为日期总是以 : (冒号)结尾,

所以我需要能够做到这在后面的代码中。

但是,我无法让它在后面的代码中工作。

我已经尝试过类似的方法(现在承认,我在 XML 方面是个新手)

IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
XPathNavigator nav = x.CreateNavigator();

XmlElement xePage = (XmlElement)nav.UnderlyingObject;
string title = xePage.GetAttribute("title");

所以我尝试了这个,但 xePage 总是将“HasAttributes”显示为 false,并且从未找到标题。 xePage.InnerXML 似乎包含正确的数据,但我不想尝试手动解析内容。

谁能指出我做错了什么的更好方向?

谢谢!

Okay so I'm pulling in an XML feed from feedburner, using an XMLDataSource and a repeater.

<asp:Repeater ID="rptrEvents" OnItemDataBound="rptrEvents_ItemDataBound" DataSourceID="XmlDataSource1" runat="server">
    <ItemTemplate>
            <li runat="server" id="liLineItem">
                <a href="<%#XPath("link")%>">
                    <span><%#XPath("pubdate")%></span>
                    <%#XPath("title")%>
                </a>
            </li>
    </ItemTemplate>
</asp:Repeater>

Now the problem I'm running into is, "pubdate" is empty, and title includes both the title and date [though it can easily be split because the date always ends in a : (colon)

So I need to be able to do this in the code behind.

However, I'm unable to get this to work in the code behind.

I've tried something like this (now granted, I'm a total newb when it comes to XML)

IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
XPathNavigator nav = x.CreateNavigator();

XmlElement xePage = (XmlElement)nav.UnderlyingObject;
string title = xePage.GetAttribute("title");

So I tried this, but xePage always shows "HasAttributes" as false, and never finds the title. xePage.InnerXML seems to contain the proper data, but I don't want to try and parse things out manually.

Can anyone point me in a better direction on what I'm doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-08-19 17:37:48

在您的 OnItemBound 事件中尝试此操作。

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
    XPathNavigator nav = x.CreateNavigator();
    XmlElement xePage = (XmlElement)nav.UnderlyingObject;
    string title = xePage.SelectSingleNode("title").InnerText;
}

In your OnItemBound event try this instead.

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
    XPathNavigator nav = x.CreateNavigator();
    XmlElement xePage = (XmlElement)nav.UnderlyingObject;
    string title = xePage.SelectSingleNode("title").InnerText;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文