此时它以相反的顺序显示项目,没有限制。 XmlNodeList可以有很多项。我想只显示最后四项。如何查找或显示列表中的最后四项?任何人?
XmlNodeList MyTestList = MyRssDocument.SelectNodes("test/holder/item");
string Title = "";
string Link = "";
for (int i = MyTestList.Count - 1; i >= 0; i--)
{
XmlNode MyTestDetail;
MyTestDetail = MyTestList.Item(i).SelectSingleNode("title");
if (MyTestDetail != null)
Title = MyTestDetail.InnerText;
else
Title = "";
MyTestDetail = MyTestList.Item(i).SelectSingleNode("link");
if (MyTestDetail != null)
Link = MyTestDetail.InnerText;
else
Link = "";
}
答案 0 :(得分:0)
您只需替换以下行:
for (int i = MyTestList.Count - 1; i >= 0; i--)
由此:
for (int i = MyTestList.Count - 1; i >= (MyTestList.Count - 4) ; i--)
答案 1 :(得分:0)
for (int i = MyTestList.Count; i >= 4; i--)
{
XmlNode MyTestDetail;
MyTestDetail = MyTestList.Item(i).SelectSingleNode("title");
if (MyTestDetail != null)
Title = MyTestDetail.InnerText;
else
Title = "";
MyTestDetail = MyTestList.Item(i).SelectSingleNode("link");
if (MyTestDetail != null)
Link = MyTestDetail.InnerText;
else
Link = "";
}