XML implementation Paging

Source: Internet
Author: User

This time, we made a Web client that provided the updated version of the map software as required. There should be not many users, and all the design is text operations, so we will not use the database, so we have to use XML to implement the list paging.

Public class xmlpage
...{
Private int _ nowpage;
Private int _ pagesize;
Private int _ recordcount;
Private int _ pagecount;

Public int nowpage... {Get... {return _ nowpage;} set... {_ nowpage = value ;}}
Public int pagesize... {Get... {return _ pagesize;} set... {_ pagesize = value ;}}
Public int recordcount... {Get... {return _ recordcount;} set... {_ recordcount = value ;}}
Public int pagecount... {Get... {return _ pagecount;} set... {_ pagecount = value ;}}

Public xmlpage ()...{}

Public datatable getdata (string filepath, string xpathtonodes, pagemodelby Model)
...{
Xmldocument xdoc = new xmldocument ();
Try
...{
Xdoc. Load (filepath );
}
Catch (xmlexception)
...{
Throw new exception ("wrong data path ");
}
Xmlelement root = xdoc. documentelement;
Xmlnodelist nodelist; // store Qualified Data
_ Recordcount = root. childnodes. count;

If (_ pagesize> 0)
...{
If (_ recordcount % _ pagesize = 0)
...{
_ Pagecount = _ recordcount/_ pagesize;
}
Else
...{
_ Pagecount = _ recordcount/_ pagesize + 1;
}
}
Else
...{
_ Pagecount = 1;
}

If (_ nowpage <1)
...{
_ Nowpage = 1;
}
Else if (_ nowpage> _ pagecount)
...{
_ Nowpage = _ pagecount;
}

If (_ nowpage = 1)
...{
If (_ pagesize = 0)
...{
Nodelist = xdoc. selectnodes (xpathtonodes );
}
Else
...{
Nodelist = xdoc. selectnodes (xpathtonodes + "[position () <=" + _ pagesize + "]");
}
}
Else
...{
If (_ nowpage> 1)
...{
Int startindex = (_ nowPage-1) * _ pagesize + 1;
Int endindex = startindex + _ pageSize-1;
Nodelist = xdoc. selectnodes (xpathtonodes + "[position ()> =" + startindex. tostring () + "and position () <=" + endindex. tostring () + "]");
}
Else
...{
Nodelist = xdoc. selectnodes (xpathtonodes );
}
}

If (nodelist. Count> 0)
...{
Using (datatable dt = new datatable ())
...{
If (model = pagemodelby. element) // use the left and right paging column of the subnode
...{
If (nodelist. Item (0). haschildnodes)
...{
Foreach (xmlnode xnode in nodelist. Item (0). childnodes) // create a table structure
...{
DT. Columns. Add (xnode. Name, typeof (string ));
}

Foreach (xmlnode xnode in nodelist) // fill in data
...{
Datarow DR = DT. newrow ();
For (INT I = 0; I <xnode. childnodes. Count; I ++)
...{
If (xnode. childnodes. Item (I). innertext! = NULL)
...{
Dr [I] = xnode. childnodes. Item (I). innertext;
}
}
DT. Rows. Add (DR );
}
}
}

If (model = pagemodelby. Attribute) // use an element as a paging Column
...{
If (nodelist. Item (0). Attributes. Count! = 0)
...{
Foreach (xmlattribute xatt in nodelist. Item (0). attributes) // create a table structure
...{
DT. Columns. Add (xatt. Name, typeof (string ));
}

Foreach (xmlnode xnode in nodelist) // fill in data
...{
Datarow DR = DT. newrow ();
For (INT I = 0; I <xnode. Attributes. Count; I ++)
...{
Dr [I] = xnode. attributes [I]. value;
}
DT. Rows. Add (DR );
}
}
}
Return DT;
}
}
Else
...{
Return new datatable ();
}
}

Public Enum pagemodelby
...{
Element,
Attribute
}
}
========================================================== ========
Frontend call

I used to bind the returned datatable data to the repeater.
In fact, you can also directly return XML nodes that meet the page conditions, and then use XSLT to display them. In this way, you can delete the datatable part built in xmlpage and directly return nodelist.

Private void bindpage (INT nowpage)
...{
// Pagination
Xmlpage mypage = new xmlpage ();
Mypage. nowpage = nowpage;
Mypage. pagesize = 25;
Using (datatable dt = mypage. getdata (BLL. version. filepath, "/updateinfos/updateinfo/General", xmlpage. pagemodelby. element ))
...{
Versionlist. datasource = DT; // versionlist is the repeat Control ID
}

// Bind paging data
Prepage = (mypage. nowpage-1). tostring ();
Nextpage = (mypage. nowpage + 1). tostring ();
Lastpage = mypage. pagecount. tostring ();
Nowpage = mypage. nowpage. tostring ();
Allpages = mypage. pagecount. tostring ();
Allrecords = mypage. recordcount. tostring ();
Perpagerecords = mypage. pagesize. tostring ();

Page. databind ();
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/godling/archive/2008/01/31/2074709.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.