Xpath within ItemDataBound for Repeater

喜你入骨 提交于 2019-12-10 21:53:39

问题


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!


回答1:


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;
}


来源:https://stackoverflow.com/questions/1814171/xpath-within-itemdatabound-for-repeater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!