Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

Parse XML to Repeater, show "Top 5"

I have a code that binds some info from a XML to a repeater. The code below works fine, but I only want to show "Top 5" elements from the XML. How can I show just some elements, not all?

<asp:Repeater runat="server" ID="repGames">
    <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "Title") %><br />
    </ItemTemplate>
</asp:Repeater>    

protected void Page_Load(object sender, EventArgs e)
{
    getElements("http://path.com/games.xml", repGames, 4);
}

void getElements(string XMLpath,Repeater objRepeater, int iTable)
{
    DataSet oDs = new DataSet();
    oDs.ReadXml(XMLpath);
    objRepeater.DataSource = oDs.Tables[iTable];
    objRepeater.DataBind();
}
0
webressurs
Asked:
webressurs
2 Solutions
 
renjurdevanCommented:
you can use xpath facility to get top 5

 XmlDocument objDoc = new XmlDocument();
objDoc.Load(XMLpath);

XmlNodeList objList = new XmlNodeList();
objList  = objDoc.SelectNodes("//NAMEOFYOURNODE/[position()<6]");

Parse thru objList  

Regards
Renju

0
 
anarki_jimbelCommented:
another solution is probably to remove all but first five rows in the dataset table.
Just llop through collection of rows and call RemoveAt(index) (DataRowCollection.RemoveAt Method )

Then bind to repeater
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now