使用xPath的asp.net RadRotator

时间:2012-10-25 14:59:45

标签: asp.net xpath telerik rad-controls

我正在使用RadRotator从谷歌获取RSS源:

     <telerik:RadRotator ID="RadRotator1" RotatorType="AutomaticAdvance" ScrollDirection="Up"
            ScrollDuration="2000" runat="server" DataSourceID="XmlDataSource1" Width="493"
            ItemWidth="493" Height="192" ItemHeight="75" FrameDuration="1" InitialItemIndex="-1"
            CssClass="rotator"> 

                    <%#   XPath("item") %>    



      </ItemTemplate>

发生的事情是,当我做

时,radgrid中没有显示任何内容
    <%#   XPath("item") %>  

请注意,我需要项目,因为这里有标题,说明等等,你可以在下面的小册子中看到

但如果我这样做

   <%#   XPath("description") %> 

我看到了描述,但该项目是我需要的......

以下是google rss xml文件的小部件:

    <rss version="2.0"><channel><generator>NFE/1.0</generator><title>malaria - Google News</title><link>http://news.google.com/news?pz=1&ned=us&hl=en&q=malaria</link><language>en</language><webMaster>news-feedback@google.com</webMaster><copyright>&copy;2012 Google</copyright><pubDate>Thu, 25 Oct 2012 14:56:08 GMT</pubDate><lastBuildDate>Thu, 25 Oct 2012 14:56:08 GMT</lastBuildDate><image><title>malaria - Google News</title><url>https://ssl.gstatic.com/news/img/logo/en_us/news.gif</url><link>http://news.google.com/news?pz=1&ned=us&hl=en&q=malaria</link></image><item><title>Malaria and Acquired Immunity - AllAfrica.com</title><link>http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNHNkVuoonL7HkcwkSoAldZPsWUvNg&url=http://allafrica.com/stories/201210251151.html</link><guid isPermaLink="false">tag:news.google.com,2005:cluster=http://allafrica.com/stories/201210251151.html</guid><pubDate>Thu, 25 Oct 2012 12:16:20 GMT</pubDate><description><table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br /><div style="padding-top:0.8em;"><img alt="" height="1" width="1" /></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;usg=AFQjCNHNkVuoonL7HkcwkSoAldZPsWUvNg&amp;url=http://allafrica.com/stories/201210251151.html"><b><b>Malaria</b> and Acquired Immunity</b></a><br /><font size="-1"><b><font color="#6f6f6f">AllAfrica.com</font></b></font><br /><font size="-1">Even though the economic impact in Zambia has not yet been quantified, it is likely to be substantial due to the number of productive days lost due to the number of productive days lost due to <b>malaria</b>. The intensity of <b>malaria</b> in an area determines the <b>...</b></font><br /><font size="-1" class="p"></font><br /><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dPdXW_JAl88mIRM&amp;ned=us"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div></font></td></tr></table></description></item>

请注意,我只是想获取项目部分,因为项目是描述的包装等。

1 个答案:

答案 0 :(得分:0)

这是您的方案的代码段:

XML数据源定义:

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://news.google.com/news?pz=1&amp;cf=all&amp;ned=us&amp;hl=en&amp;q=malaria&amp;output=rss" 
                XPath="rss/channel/item"></asp:XmlDataSource>

RadRotator定义:

        <telerik:RadRotator ID="RadRotator1" RotatorType="AutomaticAdvance" ScrollDirection="Up"
            ScrollDuration="1000"  runat="server" DataSourceID="XmlDataSource1" Width="493"
            ItemWidth="493" Height="250" ItemHeight="250" FrameDuration="2000" InitialItemIndex="-1"
            CssClass="rotator">
            <ItemTemplate>
                <div style="border: 1px solid darkgray; padding : 10px;background-color:lightgrey">
                    <a href='<%# XPath("link") %>'><%# XPath("title") %></a>
                    <div>
                        <%# XPath("description") %>
                    </div>
                </div>

            </ItemTemplate>
        </telerik:RadRotator>

在RSS Feed中访问项目的方法是通过路径rss / channel / item - 这将提供文档中的所有项目。然后在项目模板中,我只是抓住链接,标题和描述。 Item是父节点,这就是为什么

以下是上述代码的输出:

enter image description here