采用XmlDataSource 进行XML数据分页

1.使用控件形式

System.Xml.XmlDocument   doc   =   new   System.Xml.XmlDocument();
doc.Load(xml地址); 
XmlDataSource1.Data   =   doc.InnerXml;

对于xmldatasource的设置:
<asp:XmlDataSource   ID= "XmlDataSource1 "   runat= "server "   XPath= "/rss/channel/item[position() <4] "> </asp:XmlDataSource>
其中xpatch是格式~见msdn这里的意思是取出前3条

数据绑定:
<asp:Repeater   ID= "Repeater1 "   runat= "server "   DataSourceID= "XmlDataSource1 ">
                                        <ItemTemplate>
                                                <div     align= "left "> &nbsp;&nbsp;
                                                        <asp:HyperLink   ID= "HyperLink1 "   runat= "server "   CssClass= "word "   NavigateUrl= ' <%#XPath( "link ")   %> '
                                                                Target= "_blank ">
                        <%#getword(XPath( "title "))%> </asp:HyperLink> </div>
                                        </ItemTemplate>
                                </asp:Repeater>

 

2.编码形式

 

XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(rss.RssUrl);
        this.AspNetPager1.RecordCount = xmlDoc.GetElementsByTagName("item").Count;

        XmlDataSource docSource = new XmlDataSource();
        docSource.Data = xmlDoc.InnerXml;
        docSource.EnableCaching = false; //设置XmlDataSource不缓存

        int startIndex = CommonHelper.GetPageStartIndex(this.AspNetPager1.PageSize, Convert.ToInt32(Request.QueryString["page"]));
        int endIndex = startIndex + this.AspNetPager1.PageSize;
        docSource.XPath = "/rss/channel/item[position()>" + startIndex + " and position()<" + endIndex + "]";

        this.GridViewRss.DataSource = docSource;
        this.GridViewRss.DataBind();

注意:XmlDataSource 的 EnableCaching 属性,如果在分页的情况下需要设置为false,否则数据会有缓存

posted @ 2009-04-16 15:20  supers  阅读(501)  评论(0)    收藏  举报